Question

I have problem with LNK2001: unresolved external symbol error. It shows only when I have all my classes in my namespace and refers to global variables which I use multiple files. Here is example code how my codes look like:

Engine.h

#pragma once
namespace Engine{
    #include "Core.h"
    #include "Display.h"
    #include "Box.h"
    // ... some code...
}
using namespace Engine;

Core.cpp

#include "Engine.h"
// ...some code...

Core.h

extern Matrix matrix;

// ... some code...

Display.cpp

#include "Engine.h"
Matrix matrix;

// ... some code...

Display.h

// ... some code...

Box.cpp

void Box::draw(PxShape *shape){
    // matrix=.. some code...
}

Box.h

// ... some code...

Error message

1>Box.obj : error LNK2001: unresolved external symbol "struct Engine::Matrix Engine::matrix" (?matrix@Engine@@3UMatrix@1@A)

When I comment namespace everything works as it should. It is the first time when I want use a namespaces and I have no idea what to do with this.

Était-ce utile?

La solution

Your #include directives (and therefore your interface definitions) are inside namespace Engine, but it appears your implementations are not. That's giving you the link error.

You need to wrap the body of code in each of those .cpp files in namespace Engine also.

ie:

 #include "engine.h"
 namespace Engine
 {
     // implementation goes here
 }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top