Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ nested namespaces error - expected type-specifier error

Tags:

c++

namespaces

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.

like image 801
Carlos Ost Avatar asked Jan 02 '23 11:01

Carlos Ost


2 Answers

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.

like image 114
I Funball Avatar answered Jan 04 '23 01:01

I Funball


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.

like image 21
mastercat Avatar answered Jan 04 '23 00:01

mastercat