【发布时间】:2009-12-27 16:00:25
【问题描述】:
我的代码有什么问题?
我尝试在 GNU G++ 环境中编译以下代码,但出现以下错误:
朋友2.cpp:30:错误:不完整类型“结构二”的使用无效 friend2.cpp:5:错误:“结构二”的前向声明 friend2.cpp:在成员函数“int two::accessboth(one)”中: 朋友2.cpp:24:错误:“int one::data1”是私有的 friend2.cpp:55:错误:在此上下文中#include <iostream>
using namespace std;
class two;
class one
{
private:
int data1;
public:
one()
{
data1 = 100;
}
friend int two::accessboth(one a);
};
class two
{
private:
int data2;
public:
two()
{
data2 = 200;
}
int accessboth(one a);
};
int two::accessboth(one a)
{
return (a.data1 + (*this).data2);
}
int main()
{
one a;
two b;
cout << b.accessboth(a);
return 0;
}
【问题讨论】:
标签: c++ friend-function