2016-03-28 106 views
1

下面的代码使用给了我:不完全型嵌套命名空间

In member function ‘void A::method()’:error: incomplete type ‘B’ used in nested name specifier B::meth();

我寻找解决这个错误的SO发现,我可以用::但没有帮助

class B; 
class A 
{ 
    public: 
    void method() 
    { 
     B::meth(); 
    } 
}; 

class B 
{ 
    public: 
    void static meth() 
    { 
    } 
}; 
+1

你有没有试过在A班之前定义B班? –

+0

我有,但后来我有其他依赖问题,有没有其他解决方案? – lllook

+0

看看@R Sahu的答案,希望它有帮助。 –

回答

3

在生产线A::method被定义,B只有名字而不是其全部定义。

您必须确保在使用B::meth()之前已知B的完整定义。

选项1

移动的BA定义之前的定义。

class B 
{ 
    public: 
    void static meth() 
    { 
    } 
}; 

class A 
{ 
    public: 
    void method() 
    { 
     B::meth(); 
    } 
}; 

选项2

移动的B定义后的A::method定义。

class A 
{ 
    public: 
    void method(); 
}; 

class B 
{ 
    public: 
    void static meth() 
    { 
    } 
}; 

void A::method() 
{ 
    B::meth(); 
} 
0

问题是,在类B定义之前,类A试图访问类B的成员。你应该先定义B类。此外,考虑重新命名类,以便它们按字母顺序排列。

相关问题