Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Error with C++ and namespace

Here's the whole code getting the errors:

Engine.h

#ifndef ENGINE_H
#define ENGINE_H

#include "DXManager.h"

namespace XEngine
{
    class Engine
    {
    };
}

#endif

DXManager.h

#ifndef DX_MANAGER_H
#define DX_MANAGER_H



namespace XEngine
{
    class Engine; // forward declaration

    class DXManager
    {
    public:
        void run(Engine *engine);
    };
}

#endif

DXManager.cpp

#include <iostream>

#include "Engine.h"
#include "DXManager.h"

using namespace XEngine;

void DXManager::run(Engine *engine)
{
    std::cout<<"DXManager::run"<<std::endl;
}

With these 30 lines of code, I'm getting 20 errors like:

'XEngine' : a namespace with this name does not exist
'XEngine' : a symbol with this name already exists and therefore this name cannot be used as a namespace name
syntax error : identifier 'Engine'

Clearly, I'm missing something important here. What am I doing wrong?

note: I am aware that circular dependency is a bad thing, but in my particular case I believe that it is relevant.

like image 657
Jesse Emond Avatar asked Dec 09 '25 22:12

Jesse Emond


1 Answers

In DXManager.cpp you are not just using some names from namespace XEngine. You define the function in that namespace.

So must be:

DXManager.cpp

#include <iostream>

#include "Engine.h"
#include "DXManager.h"

namespace XEngine {

void DXManager::run(Engine *engine)
{
    std::cout<<"DXManager::run"<<std::endl;
}

}

AFAIK some of the compilers (like MSVC) process using variant too. But it is not correct because your syntax tries to define function ::DXManager::run - not ::XEngine::DXManager::run you intend to define.

like image 144
Serge Dundich Avatar answered Dec 11 '25 10:12

Serge Dundich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!