请教各位朋友,问题出在哪???
我写了一个测试程序,证明一个类的友元函数可以访问该类的私有数据成员,可是结果却显示不能访问该类的私有数据成员,代码如下,请教各位朋友问题出在哪里??
#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)
#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)
...全文
相关推荐
Fortran编译程序出现问题怎么解决???_
Fortran编译程序出现问题怎么解决???_
CAD字体显示???问号解决工具
标题提到的“CAD字体显示???问号解决工具”是一个专门针对这种情况设计的AutoLISP程序,它能够帮助用户快速有效地解决这个问题。
求教类似SUMIF;COUNTIF的条件函数的写法???
求教类似SUMIF;COUNTIF的条件函数的写法???
就一定要起这个名字吗???
就一定要起这个名字吗???
FPGA扇出问题
FPGA扇出问题FPGA扇出问题是指门电路带负载的能力,即一个门电路的输出能同时驱动几个门电路。扇出问题在FPGA设计中是非常重要的,因为它会严重影响FPGA布线的稳定性。


