【问题标题】:forward declaration is not accepted [duplicate]不接受前向声明[重复]
【发布时间】:2015-01-06 11:40:08
【问题描述】:
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 中引发以下错误。

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

【问题讨论】:

    标签: c++ friend-function


    【解决方案1】:

    编译器解析语句时

    friend int B::test();
    

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

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

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2018-04-16
      • 2011-08-29
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多