• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉自:http://hi.baidu.com/%C4%CF%B7%C9%D1%E3%D1%E3%C4%CF%B7%C9/blog/item/4722d43c53868b09bba1672e.html

            舉個例子:

            #include<iostream> 
            using namespace std; 
            class cylinder 

                friend istream 
            operator>>(istream& is,cylinder &cy); 
            public:     
                inline 
            double square() 
                
            {       return length*(width+height)*2+width*height*2;    } 
                inline 
            double volume() 
                
            {      return length*width*height;      } 
            private
                
            double length; 
                
            double width; 
                
            double height; 
            }

            istream 
            operator>>(istream is,cylinder &cy) 

                cout
            <<"input length:"<<endl; 
                
            is>>cy.length; 
                cout
            <<"input width:"<<endl; 
                
            is>>cy.width; 
                cout
            <<"input height:"<<endl; 
                
            is>>cy.height; 
                
            return is
            }
             
            int main() 

                cylinder first; 
                cin
            >>first; 
                cout
            <<first.square()<<endl; 
                cout
            <<first.volume()<<endl; 
                
            return 0
            }

            這些代碼在VC6.0中不能被編譯通過:提示不能訪問私有成員,沒有這個訪問權限

            改成這樣就可以了,代碼如下:(我用的這個方法,但是得把變量改成public才行)

            #include<iostream> 
            using std::cin; 
            using std::endl; using std::cout; 
            using std::ostream; 
            using std::istream; 
            using std::ostream; 
            class cylinder 

                friend istream 
            operator>>(istream& is,cylinder &cy); 
            public:     
                inline 
            double square() 
                
            {       return length*(width+height)*2+width*height*2;    } 
                inline 
            double volume() 
                
            {      return length*width*height;      } 
            private
                
            double length; 
                
            double width; 
                
            double height; 
            }

            istream 
            operator>>(istream is,cylinder &cy) 

                cout
            <<"input length:"<<endl; 
                
            is>>cy.length; 
                cout
            <<"input width:"<<endl; 
                
            is>>cy.width; 
                cout
            <<"input height:"<<endl; 
                
            is>>cy.height; 
                
            return is
            }
             
            int main() 

                cylinder first; 
                cin
            >>first; 
                cout
            <<first.square()<<endl; 
                cout
            <<first.volume()<<endl; 
                
            return 0
            }

            原因:

            這據說是VC的一個經典BUG。和namespace也有關.

            只要含有using namespace std; 就會提示友員函數沒有訪問私有成員的權限。

            解決方法:去掉using namespace std;換成更小的名字空間。

            例如:
            含有#include <string>就要加上using std::string
            含有#include <fstream>就要加上using std::fstream
            含有#include <iostream>就要加上using std::cin; using std::cout; using std::ostream; using std::istream; using std::endl; 等等,需要什么即可通過using聲明什么.

            下面給出流浪給的解決辦法:

            //方法一:
            //提前聲明
            class cylinder;
            istream &operator>>(istream& is,cylinder &cy);

            //方法二:
            //不用命名空間 或者 像晨雨那樣寫
            #include<iostream.h>

            //方法三:

            class cylinder
            {
                friend istream &operator>>(istream& is,cylinder &cy)//寫在類里面
                {
                    cout<<"input length:"<<endl;
                    is>>cy.length;
                    cout<<"input width:"<<endl;
                    is>>cy.width;
                    cout<<"input height:"<<endl;
                    is>>cy.height;
                    return is;
                   
                }
            ..........
            //方法四:打SP6補丁,貌似不好使。。。(呵呵,是貌似也沒用)

            //方法五:換別的對標準C++支持好的編譯器,如DEV C++/。。。(呵呵)

            本文來自CSDN博客,轉載:http://blog.csdn.net/zgjxwl/archive/2008/10/13/3067973.aspx

            posted on 2010-02-15 02:56 煙皚 閱讀(1051) 評論(1)  編輯 收藏 引用 所屬分類: C++ primer plus學習筆記

            Feedback

            # re: VC6.0中友元函數無法訪問類私有成員的解決辦法-------VC6.0的bug 2010-09-19 22:36 吳冬亮
            sp6補丁裝了就ok了

            地址:
            http://download.microsoft.com/download/1/9/f/19fe4660-5792-4683-99e0-8d48c22eed74/Vs6sp6.exe

            就算認為自己裝過了 也請重裝遍 肯定行!  回復  更多評論
              

            久久久久女教师免费一区| 激情伊人五月天久久综合| 久久精品国产亚洲AV不卡| 一本大道久久东京热无码AV| 久久久亚洲裙底偷窥综合| 久久精品a亚洲国产v高清不卡| 国产成人AV综合久久| 国产精品成人久久久| 久久青青草原综合伊人| 亚洲中文字幕伊人久久无码| 久久大香香蕉国产| 欧美精品一区二区久久| 国产精品禁18久久久夂久| 久久综合九色综合欧美就去吻 | 久久精品蜜芽亚洲国产AV| 日本三级久久网| 久久精品国产亚洲av高清漫画| 欧美成a人片免费看久久| 久久综合丁香激情久久| 久久亚洲精品无码AV红樱桃| 亚洲欧洲中文日韩久久AV乱码| 国产一级持黄大片99久久| 久久精品国产AV一区二区三区 | 亚洲国产精品无码久久SM| segui久久国产精品| 国产91色综合久久免费分享| 久久九九兔免费精品6| 麻豆国内精品久久久久久| 国产成人精品综合久久久| 久久精品毛片免费观看| 国产精品99久久久精品无码| 热久久国产欧美一区二区精品| 色综合久久综精品| 久久久久免费精品国产| 热99re久久国超精品首页| AV无码久久久久不卡网站下载 | 精品欧美一区二区三区久久久| 久久精品国产亚洲av麻豆色欲| 亚洲欧美成人综合久久久| 午夜人妻久久久久久久久| 久久亚洲精品国产精品|