class two;
class one
{
int a;
public:
one()
{
a = 8;
}
friend two;
};
class two
{
public:
two() { }
two(one i)
{
cout << i.a;
}
};
int main()
{
one o;
two t(o);
getch();
}
我从Dev-C ++中收到此错误:
a class-key must be used when declaring a friend
但是使用Microsoft Visual C ++编译器编译时运行正常。
答案 0 :(得分:12)
你需要
friend class two;
而不是
friend two;
此外,您不需要单独向前声明您的类,因为friend-declaration本身就是一个声明。你甚至可以这样做:
//no forward-declaration of two
class one
{
friend class two;
two* mem;
};
class two{};
答案 1 :(得分:5)
您的代码有:
friend two;
应该是:
friend class two;