c++ - 嵌套命名空间中使用的不完整类型

标签 c++

下面的代码给我:

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()
    {
    }
};

最佳答案

在定义 A::method 行,B 仅通过名称而不是其完整定义而为人所知。

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

选项 1

B 的定义移到 A 的定义之前。

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

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

选项 2

A::method 的定义移到 B 的定义之后。

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

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

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

关于c++ - 嵌套命名空间中使用的不完整类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36256111/

相关文章:

c++ - 分而治之真的能战胜增加的内存分配吗?

c++ - <C++> 构造代码输出

c++ - 已删除的拷贝分配检测到虚假错误?

C++ 编译时条件运行时语句

C++ Qt 多重定义

C++ - 泛型类型,其中类型变量必须具有特定类型

c++ - CUDA 链接错误 Visual Studio 2008

c++ - C++ 中多线程的安全性与速度

c++ - 使用 std::function 时如何解决此 <unresolved overloaded function type> 错误?

c++ - Lambda 以迭代器为参数