请教各位朋友,问题出在哪???

papaofdoudou papaofdoudou 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我写了一个测试程序,证明一个类的友元函数可以访问该类的私有数据成员,可是结果却显示不能访问该类的私有数据成员,代码如下,请教各位朋友问题出在哪里??
#include <iostream>
using namespace std;




class base
{
friend void display1(base);

public:
friend istream& operator>>(istream& input, base& string);
base(int x=1024)
{
a=x;
}
void display()
{
cout<<a<<endl;
}
private:
int a;
};

void display1(base x) //友元函数的定义
{
cout<<"The color of the car is : "<<x.a<<endl;
}

istream& operator>>(istream& input, base& string)
{
input>>string.a; //访问私有数据成员
cout<<string.a; //访问私有数据成员
return input;

}

void main(void)
{
base mm;
// cin>>mm;
mm.display();
}

编译错误显示:
Compiling...
1.cpp
F:\Program\cha2\1.cpp(33) : error C2248: 'a' : cannot access private member declared in class 'base'
F:\Program\cha2\1.cpp(23) : see declaration of 'a'
F:\Program\cha2\1.cpp(34) : error C2248: 'a' : cannot access private member declared in class 'base'
F:\Program\cha2\1.cpp(23) : see declaration of 'a'
执行 cl.exe 时出错.

cha2.exe - 1 error(s), 0 warning(s)
...全文
评论 4
收藏 已收藏
分享
返回顶部
papaofdoudou
papaofdoudou 新星创作者: 人工智能技术领域 新星创作者: 人工智能技术领域领域专家: 嵌入式与硬件开发技术领域 领域专家: 嵌入式与硬件开发技术领域
C++ 语言 65,212 成员 250,513 内容