Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
There is absolutely no reason to define a namespace Engine if in the same header file that defines this namespace, you introduce all elements of the Engine namespace into the global namespace. –  Oswald Jul 6 '13 at 17:27
    
Before defining a namespace I have also included some 3rd party libraries and standard c++ headers and I wanted to indicate that these classes are provided by me. –  Harry Jul 6 '13 at 17:55

1 Answer 1

up vote 1 down vote accepted

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
 }
share|improve this answer
    
Thanks, I thought .cpp files are automatically included into headers namespace. –  Harry Jul 6 '13 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.