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

            在Visual C++中定義一般的函數(shù)為朋友函數(shù)通常是沒有問題的。
            然而對某些重載操作符的函數(shù),
            即使我們將它們定義為類的朋友函數(shù),VC的編譯器仍然會顯示出錯信息,
            認為這些朋友函數(shù)無權(quá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;
                        };
            
            
            // 實現(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++中編譯運行毫無問題。但是在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中解決這個問題有以下幾種方法:

            • 在頭文件中實現(xiàn)作為朋友函數(shù)的操作符函數(shù)的重載,也就是說在實現(xiàn)文件”Sample.cpp”中將函數(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ù)的原型特別聲明一下,也就是將頭文件修改如下(實現(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名空間的使用實行明確聲明,也就是說在頭文件”Sample.h”中直接寫:
              #include<iostream>
              using std::ostream;
              using std::istream
              ….
              取代 “using namespace std;”
              注意:在這個例子里我們在實現(xiàn)文件 “Sample.cpp”中包含 “using namespace std;”這句話,否則在實現(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 閱讀(2370) 評論(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
            一本久久精品一区二区| 久久亚洲精品成人无码网站 | 99久久香蕉国产线看观香| 久久精品国产精品亚洲人人| 久久综合九色欧美综合狠狠| 久久精品卫校国产小美女| 精品无码久久久久国产| 国产AV影片久久久久久| 久久久久久久综合狠狠综合| 久久精品国产亚洲av高清漫画| 国产91久久综合| 热久久最新网站获取| 99久久中文字幕| 少妇无套内谢久久久久| 国产Av激情久久无码天堂| 大香伊人久久精品一区二区| 久久久久久综合一区中文字幕 | 国产精品一区二区久久不卡| 久久久人妻精品无码一区 | 国产成人久久精品麻豆一区| 日产精品久久久久久久| 国产精品美女久久久久AV福利| 精品一二三区久久aaa片| 99久久精品免费| 久久99精品久久久久久久不卡 | 精品久久久久久无码专区不卡| 午夜精品久久影院蜜桃| 国产69精品久久久久99| 国产精品久久影院| 77777亚洲午夜久久多喷| 99久久精品国产一区二区| 久久婷婷色香五月综合激情| 精品国产综合区久久久久久| 女人香蕉久久**毛片精品| av午夜福利一片免费看久久| 99久久久国产精品免费无卡顿| 久久一日本道色综合久久| 久久久久人妻一区精品色| 久久精品亚洲一区二区三区浴池 | 亚洲精品美女久久777777| 亚洲精品无码专区久久久|