VC6.0中友元函數無法訪問類私人成員的解決辦法——-VC6.0的bug

來源:互聯網
上載者:User

舉個例子:

 

  1. #include<iostream>
  2. using namespace std;
  3. class cylinder
  4. {
  5.     friend istream operator>>(istream& is,cylinder &cy);
  6. public:    
  7.     inline double square()
  8.     {       return length*(width+height)*2+width*height*2;    }
  9.     inline double volume()
  10.     {      return length*width*height;      }
  11. private:
  12.     double length;
  13.     double width;
  14.     double height;
  15. };
  16. istream operator>>(istream is,cylinder &cy)
  17. {
  18.     cout<<"input length:"<<endl;
  19.     is>>cy.length;
  20.     cout<<"input width:"<<endl;
  21.     is>>cy.width;
  22.     cout<<"input height:"<<endl;
  23.     is>>cy.height;
  24.     return is;
  25. }
  26. int main()
  27. {
  28.     cylinder first;
  29.     cin>>first;
  30.     cout<<first.square()<<endl;
  31.     cout<<first.volume()<<endl;
  32.     return 0;
  33. }

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

改成這樣就可以了,代碼如下:

 

  1. #include<iostream>
  2. using std::cin;
  3. using std::endl; using std::cout;
  4. using std::ostream;
  5. using std::istream;
  6. using std::ostream;
  7. class cylinder
  8. {
  9.     friend istream operator>>(istream& is,cylinder &cy);
  10. public:    
  11.     inline double square()
  12.     {       return length*(width+height)*2+width*height*2;    }
  13.     inline double volume()
  14.     {      return length*width*height;      }
  15. private:
  16.     double length;
  17.     double width;
  18.     double height;
  19. };
  20. istream operator>>(istream is,cylinder &cy)
  21. {
  22.     cout<<"input length:"<<endl;
  23.     is>>cy.length;
  24.     cout<<"input width:"<<endl;
  25.     is>>cy.width;
  26.     cout<<"input height:"<<endl;
  27.     is>>cy.height;
  28.     return is;
  29. }
  30. int main()
  31. {
  32.     cylinder first;
  33.     cin>>first;
  34.     cout<<first.square()<<endl;
  35.     cout<<first.volume()<<endl;
  36.     return 0;
  37. }

原因:

這據說是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++/。。。(呵呵)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.