• <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++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(8)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            轉(zhuǎn)自: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中不能被編譯通過:提示不能訪問私有成員,沒有這個訪問權(quán)限

            改成這樣就可以了,代碼如下:(我用的這個方法,但是得把變量改成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
            }

            原因:

            這據(jù)說是VC的一個經(jīng)典BUG。和namespace也有關(guān).

            只要含有using namespace std; 就會提示友員函數(shù)沒有訪問私有成員的權(quán)限。

            解決方法:去掉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補丁,貌似不好使。。。(呵呵,是貌似也沒用)

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

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

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

            Feedback

            # re: VC6.0中友元函數(shù)無法訪問類私有成員的解決辦法-------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

            就算認(rèn)為自己裝過了 也請重裝遍 肯定行!  回復(fù)  更多評論
              

            久久精品国产亚洲AV高清热| 国产日韩久久免费影院| 久久婷婷激情综合色综合俺也去| 国产亚洲精品久久久久秋霞| A狠狠久久蜜臀婷色中文网| 久久精品国产亚洲7777| 国产毛片欧美毛片久久久| 成人国内精品久久久久影院VR| 综合久久一区二区三区| 99热热久久这里只有精品68| 99蜜桃臀久久久欧美精品网站 | 久久久国产精华液| 久久91精品久久91综合| 97精品国产97久久久久久免费| 99久久成人18免费网站| 久久久久亚洲av无码专区| 亚洲人成无码久久电影网站| 色偷偷888欧美精品久久久| 日韩av无码久久精品免费| 久久婷婷五月综合成人D啪| 久久青青国产| 欧美久久一级内射wwwwww.| 国产精品九九久久免费视频 | 男女久久久国产一区二区三区| 少妇被又大又粗又爽毛片久久黑人| 国内精品久久久久伊人av| 国产亚洲精久久久久久无码77777| 无码乱码观看精品久久| 日韩欧美亚洲国产精品字幕久久久| 丁香久久婷婷国产午夜视频| 青青青伊人色综合久久| 青青草国产精品久久| 99久久久精品免费观看国产| 国产精品国色综合久久| 国产精品99久久99久久久| 久久福利青草精品资源站免费| 99精品国产在热久久| 香蕉久久夜色精品国产小说| 99久久精品免费| 中文成人久久久久影院免费观看| 无码人妻久久一区二区三区蜜桃|