• <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>
            隨筆 - 30, 文章 - 0, 評論 - 64, 引用 - 0
            數(shù)據(jù)加載中……

            VC6.0中重載操作符函數(shù)無法訪問類的私有成員

            在 C++ 中,操作符(運(yùn)算符)可以被重載以改寫其實(shí)際操作。
            同時我們可以定義一個函數(shù)為類的朋友函數(shù)(friend function)以便使得這個函數(shù)能夠訪問類的私有成員,
            這個定義通常在頭文件中完成。

            在Visual C++中定義一般的函數(shù)為朋友函數(shù)通常是沒有問題的。
            然而對某些重載操作符的函數(shù),
            即使我們將它們定義為類的朋友函數(shù),VC的編譯器仍然會顯示出錯信息,
            認(rèn)為這些朋友函數(shù)無權(quán)訪問類的私有成員。
            我認(rèn)為這應(yīng)該是VC6.0的bug。

            以下代碼就是個例子:

            // 頭文件 “Sample.h”
                        #include<iostream>
                        using namespace std;
                        class Sample {
                        public:
                        Sample();
                        friend ostream &operator<<(ostream &out, const Sample s);
                        friend istream &operator>>(istream &in, Sample & s);
                        private:
                        int x;
                        };
            
            
            // 實(shí)現(xiàn)文件 “Sample.cpp”
                        #include “Sample.h”
                        Sample::Sample() {
                        x=0;
                        }
                        istream &operator>>(istream &in, Sample & s) {
                        cout<<”Please enter a value”<<endl;
                        in >> s.x ;
                        return in;
                        }
                        ostream &operator<<(ostream &out, const Sample s) {
                        cout << s.x << endl;
                        return out;
                        }

            以上代碼在gnuc++中編譯運(yùn)行毫無問題。但是在VC++6.0中編譯的時候就會出現(xiàn)以下的編譯錯誤:

            Compiling…
            Sample.cpp
            c:\temp\sample.cpp(8) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
            c:\temp\sample.h(19) : see declaration of ‘x’
            c:\temp\sample.cpp(13) : error C2248: ‘x’ : cannot access private member declared in class ‘Sample’
            c:\temp\sample.h(19) : see declaration of ‘x’
            Error executing cl.exe.Sample.obj - 2 error(s), 0 warning(s)

            在VC++ 6.0中解決這個問題有以下幾種方法:

            • 在頭文件中實(shí)現(xiàn)作為朋友函數(shù)的操作符函數(shù)的重載,也就是說在實(shí)現(xiàn)文件”Sample.cpp”中將函數(shù)重載的實(shí)現(xiàn)去掉,而將頭文件修改如下:
              // 修改后的頭文件 1 “Sample.h”
                              #include<iostream>
                              using namespace std;
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              friend ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }
                              friend istream &operator>>(istream &in, Sample & s);
                              friend istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }
                              private:
                              int x;
                              };
              
                  
            • 在頭文件中類定義之前將類和朋友操作符函數(shù)的原型特別聲明一下,也就是將頭文件修改如下(實(shí)現(xiàn)文件”Sample.cpp”不用作任何修改):
              // 修改后的頭文件 2 “Sample.h”
                              #include<iostream>
                              using namespace std;
                              // 以下3行代碼為新加入
                              class Sample;
                              ostream &operator<<(ostream &out, const Sample s);
                              istream &operator>>(istream &in, Sample & s);
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              friend istream &operator>>(istream &in, Sample & s);
                              private:
                              int x;
                              };
              
                  
            • 第三種方法是對I/O名空間的使用實(shí)行明確聲明,也就是說在頭文件”Sample.h”中直接寫:
              #include<iostream>
              using std::ostream;
              using std::istream
              ….
              取代 “using namespace std;”
              注意:在這個例子里我們在實(shí)現(xiàn)文件 “Sample.cpp”中包含 “using namespace std;”這句話,否則在實(shí)現(xiàn)中就不能使用 “cout” , “cin”, “<< “, “>>” 和 endl 這些關(guān)鍵字和符號。修改后的完整代碼如下:

               

              // Sample.h
                              #include<iostream>
                              using std::istream;
                              using std::ostream;
                              class Sample {
                              public:
                              Sample();
                              friend ostream &operator<<(ostream &out, const Sample s);
                              /*friend ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }*/
                              friend istream &operator>>(istream &in, Sample & s);
                              /*friend istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }*/
                              private:
                              int x;
                              };
              // “Sample.cpp”
                              #include “Sample.h”
                              using namespace std;
                              Sample::Sample() {
                              x=5;
                              }
                              istream &operator>>(istream &in, Sample & s) {
                              cout<<”Please enter a value”<<endl;
                              in >> s.x ;
                              return in;
                              }
                              ostream &operator<<(ostream &out, const Sample s) {
                              cout << s.x << endl;
                              return out;
                              }
              
                  

            posted on 2008-12-08 23:50 henry08 閱讀(2361) 評論(5)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: VC6.0中重載操作符函數(shù)無法訪問類的私有成員  回復(fù)  更多評論   

            能講解一下其內(nèi)在原因是什么嗎?
            2008-12-09 09:45 | abettor

            # re: VC6.0中重載操作符函數(shù)無法訪問類的私有成員  回復(fù)  更多評論   

            猜測的成分太多了,friend function好像不是翻成朋友函數(shù),叫友元吧
            2008-12-09 09:51 | zuhd

            # re: VC6.0中重載操作符函數(shù)無法訪問類的私有成員  回復(fù)  更多評論   

            visual c++ 2005 就支持了
            2008-12-10 21:45 | 漂漂

            # re: VC6.0中重載操作符函數(shù)無法訪問類的私有成員  回復(fù)  更多評論   

            hanxiaodkf
            2009-07-14 16:50 | han

            # re: VC6.0中重載操作符函數(shù)無法訪問類的私有成員[未登錄]  回復(fù)  更多評論   

            Thanks
            2012-06-25 09:43 | yong
            久久只有这里有精品4| 久久精品国产精品亚洲精品| 7777精品伊人久久久大香线蕉| 一本色道久久88综合日韩精品 | 18岁日韩内射颜射午夜久久成人| 精品久久久无码人妻中文字幕 | 怡红院日本一道日本久久 | 亚洲成人精品久久| 久久成人精品| 性做久久久久久久| 精品久久国产一区二区三区香蕉| 精品人妻伦九区久久AAA片69 | 久久精品a亚洲国产v高清不卡| 91久久精品电影| 久久精品国产亚洲AV大全| 久久精品成人| 久久婷婷久久一区二区三区| 国内精品伊人久久久久网站| 久久国产欧美日韩精品| 国产高清国内精品福利99久久| 欧美va久久久噜噜噜久久| 一本色综合久久| 久久精品无码av| 999久久久免费国产精品播放| 色欲久久久天天天综合网 | 亚洲精品无码久久久久| 久久久WWW免费人成精品| 四虎国产精品免费久久5151| 久久综合狠狠综合久久综合88| 国产成人精品久久| 久久精品国产欧美日韩99热| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久精品国产精品亜洲毛片| 久久久国产精品网站| 国产成人久久精品一区二区三区| 狠狠综合久久AV一区二区三区| 少妇无套内谢久久久久| 国产aⅴ激情无码久久| 色综合久久久久久久久五月| 99999久久久久久亚洲| 精品久久久久久久久中文字幕|