简体   繁体   中英

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

#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

#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

The error is error: expected type-specifier b = new CLASS_B::Class_B::Class_B();

The compiler is pointing to the namespace 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:

CLASS_B::Class_B()* b;

If you are allocating a block of memory with new you need a pointer to point to that block.

You are missing the type of b in the declaration which must be specified before the identifier name. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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