C++ 嵌套命名空间错误 - 预期的类型说明符错误

标签 c++ namespaces

我一直在寻找这个问题的解决方案,我认为它与嵌套命名空间有关。

下面我们有它的相关部分:

implementation.hpp 这是一个接口(interface)的实现

#ifndef IMPLEMENTATION_H
#define IMPLEMENTATION_H

#include "class_b.hpp"

namespace XPTO {
class Implementation : public XPTO::Interface {

    public:

        Implementation();
        ~Implementation() override;

        ReturnStatus
        initialize() override;

    private:

        CLASS_B::Class_B b; // namespace CLASS_B, class Class_B
    };
}
#endif

实现.cpp

#include "implementation.hpp"

XPTO::Implementation::Implementation() {}

XPTO::ReturnStatus
XPTO::Implementation::initialize() {
    b = new CLASS_B::Class_B::Class_B(); 
    //namespace ClASS_B, class Class_B and constructor Class_B()
}

class_b.hpp

#ifndef CLASS_B_H
#define CLASS_B_H

namespace CLASS_B{

class Class_B {

    public:

        Class_B();
        ~Class_B();

        void initialize();
    };
}
#endif

错误是 错误:预期的类型说明符 b = new CLASS_B::Class_B::Class_B();

编译器指向命名空间 CLASS_B。

最佳答案

我认为这一行是你的问题:

b = new CLASS_B::Class_B::Class_B(); 

它只需要是:

b = new CLASS_B::Class_B(); 

假设 b 已经在其他地方声明:

CLASS_B::Class_B()* b;

如果您使用 new 分配一 block 内存,您需要一个指向该 block 的指针。

关于C++ 嵌套命名空间错误 - 预期的类型说明符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54041604/

相关文章:

kubernetes - 让 prometheus/grafana 和 k3s 协同工作

用于存储(插入/获取/删除)具有整数值多键的结构的 C++ 容器是指针

c++ - 从 clang 中的 FunctionDecl 类获取参数信息

c++ linux accept()在套接字关闭后阻塞

java - 如果我们在运行时在 HBase 中创建 namespace 和表,它会影响应用程序的性能吗?

xml - 具有命名空间的 PowerShell XML 选择节点

c++ - 向 Qt 树模型中添加一些自定义数据

c++ - 带有 std::conditional 的模板函数自动返回类型

C++ 命名空间/标识符问题

internet-explorer - Vista/Server 2008 上缺少 WMI 命名空间