不接受前瞻性声明

时间:2015-01-06 11:40:09

标签: c++ friend-function

class B;
class A
{
    int divident,divisor;
    friend int B::test();
public:
     A(int i,int j):divident(i),divisor(j){}
};

class B
{
public:
    int test();
};

int B::test(){}
int main(){return 1;}

使用Mingwin在Qt Creator中抛出以下错误。

enter image description here

为什么B类前向声明​​会抛出错误? 可能是我犯了一个愚蠢的错误,但我无法找到它。

1 个答案:

答案 0 :(得分:1)

当编译器正在解析语句

friend int B::test();

它不知道B类是否有成员函数测试。在这一点上,编译器需要B类的定义来确定此语句是否正确。

将B类的定义放在A类的定义之前。