简体   繁体   English

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

[英]C++ nested namespaces error - expected type-specifier error

I've being searching for the solution of this problem, and I think it has something due to nested namespaces. 我正在寻找此问题的解决方案,并且由于嵌套的命名空间,我认为它有所帮助。

Bellow we have the relevant part of it: 在下面,我们有相关的部分:

implementation.hpp That is an implementation of an Interface Implementation.hpp这是一个接口的实现

#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

implementation.cpp implementation.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 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

The error is error: expected type-specifier b = new CLASS_B::Class_B::Class_B(); 错误是错误:预期的类型说明符b =新的CLASS_B :: Class_B :: Class_B();

The compiler is pointing to the namespace CLASS_B. 编译器指向命名空间CLASS_B。

I think this line is your problem: 我认为这是您的问题:

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

It only needs to be: 它只需要是:

b = new CLASS_B::Class_B(); 

Assuming b is already declared somewhere else: 假设b已经在其他地方声明:

CLASS_B::Class_B()* b;

If you are allocating a block of memory with new you need a pointer to point to that block. 如果使用new分配内存块,则需要一个指向该内存块的指针。

You are missing the type of b in the declaration which must be specified before the identifier name. 您缺少声明中必须在标识符名称之前指定的b类型。 Try changing 尝试改变

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

to

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

If you intended to initialize the private member b in the initialize() method then you would need to declare it as a pointer, since new returns a pointer. 如果打算在initialize()方法中初始化私有成员b,则需要将其声明为指针,因为new返回了指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM