• <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í)我們可以定義一個(gè)函數(shù)為類的朋友函數(shù)(friend function)以便使得這個(gè)函數(shù)能夠訪問類的私有成員,
            這個(gè)定義通常在頭文件中完成。

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

            以下代碼就是個(gè)例子:

            // 頭文件 “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中編譯的時(shí)候就會出現(xiàn)以下的編譯錯(cuò)誤:

            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中解決這個(gè)問題有以下幾種方法:

            • 在頭文件中實(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;”
              注意:在這個(gè)例子里我們在實(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 閱讀(2374) 評論(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久久亚洲综合精品 | 精品国产91久久久久久久| 国产精品久久久久久久久久免费| 久久久久久一区国产精品| 亚洲国产精品一区二区久久hs| 欧美久久综合性欧美| 久久精品国产亚洲AV不卡| 国产成人无码精品久久久免费 | 亚洲国产精品无码久久久蜜芽| 狠狠久久综合| 久久综合久久久| 人妻丰满AV无码久久不卡| 久久青青草视频| 久久久久人妻精品一区三寸蜜桃| 久久国产精品成人影院| 久久亚洲中文字幕精品一区| 久久久久国产视频电影| 青青草国产精品久久久久| 亚洲成色www久久网站夜月| 久久这里只精品99re66| 亚洲国产成人久久综合一区77| 亚洲国产精品久久久久婷婷软件| 久久综合香蕉国产蜜臀AV| 国内精品九九久久精品| 久久精品国产日本波多野结衣| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 一级做a爰片久久毛片16| 久久av无码专区亚洲av桃花岛| 久久精品青青草原伊人| 久久精品极品盛宴观看| 国产精品久久婷婷六月丁香| 91麻豆国产精品91久久久| 亚洲天堂久久久| 伊人久久大香线蕉av不变影院| 2020久久精品亚洲热综合一本| 中文字幕精品久久久久人妻| 久久婷婷是五月综合色狠狠| 久久久久久久久波多野高潮|