不接受前向声明

forward declaration is not accepted

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 中抛出以下错误。

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

编译器解析语句时

friend int B::test();

不知道classB是否有成员函数test。此时编译器需要classB的定义来判断这条语句是否正确。

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