3

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.

3
  • 2
    Your code compiles on MSVC 10 after the edit. It must be another error in another piece of code. Commented Apr 4, 2011 at 2:24
  • Indeed. I guess I didn't do the appropriate modification on my real code. Thanks a lot for your help! Commented Apr 4, 2011 at 2:28
  • Are the errors coming from dxmanager.cpp or from another compilation unit that includes engine.h and/or dxmanager.h? Commented Apr 4, 2011 at 2:46

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm taking a mental note of this. Thanks for the information.
2

In the forward-declaration of class Engine the namespace XEngine doesn't exist at this point.

A workaround would be moving the declaration inside the namespace block.

1 Comment

Thanks for pointing it out. Still 10 errors left though.. =/ The same type of errors.
1

When Engine.h includes DXManager.h, the latter defines a class XEngine::Engine without declaring the namespace first.

1 Comment

I moved the forward declaration within the namespace, but I'm still getting the errors though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.