Sign in to follow this  
rocklobster

Unresolved externals using namespace in C++

Recommended Posts

Hi,

I'm getting unresolved external symbols when using classes which are apart of a namespace (located in a separate folder).

This is a stripped down version main class i've written thats apart of its own VS2010 project.

[code]main.cpp
#include <Engine/input.h>
#include <Engine/graphics.h>
#include <Engine/sound.h>

using namespace Engine;



int main()
{
InputManager* input = new InputManager();
input->initialise();

etc.
}[/code]

It is including classes which are in a different folder and defined like so...

[code]

#ifndef INPUTMGR_H
#define INPUTMGR_H

namespace Engine
{
class InputManager
{
void initialize();
// rest of functions
};
}
#endif[/code]

and the cpp would be like..

[code]#include "InputManager.h"
namespace Engine
{
void InputManager::initialise()
{
// setup keyboard and mouse etc
}
}[/code]

I'm getting unresolved external errors to the functions that i call such as initialise(). Now i have told visual studio to look for source and include directories to this folder (because i can include and compile them so it works) but i'm still getting these errors. I should also add that putting the source code in the same folder as main.cpp works fine, but i'd like to not do this because that means each test program will have a dated copy of the engine (That is still currently in progress).

Any one got any ides or a better solution to what i'm trying to achieve?

Much appreciated,

- rocklobster

Share this post


Link to post
Share on other sites
The code you posted works fine, assuming you set up your project correctly to actually have the .cpp file in the project.

I'm not really clear on what the difference is between what you posted and your actual situation, so divining the true cause of your compile errors is going to be tricky at best and more likely impossible.

Share this post


Link to post
Share on other sites
You mentioned that you're using subdirectories. Do you have source files with the same name, but in different directories? If so, MSVC may be assigning the same name and location for the generated object files for those source files, which means effectively that only one of them is being compiled.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1333156010' post='4926835']
You mentioned that you're using subdirectories. Do you have source files with the same name, but in different directories? If so, MSVC may be assigning the same name and location for the generated object files for those source files, which means effectively that only one of them is being compiled.
[/quote]

I can't say for sure that this isn't a possibility, but I do this all the time (Foo.cpp in /Some/Path and Foo.cpp in /Other/Path) and it works fine. MSVC is smart enough to number the .obj files so that they don't collide, at least in my experience.

Share this post


Link to post
Share on other sites
Really? I just double checked to see if MSVC 2010 was smart enough to do this and it wasn't. It generates a "LNK4042: object specified more than once; extras ignored" unless you manually number the object files or stick them in subdirectories yourself.

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1333200655' post='4926927']
Really? I just double checked to see if MSVC 2010 was smart enough to do this and it wasn't. It generates a "LNK4042: object specified more than once; extras ignored" unless you manually number the object files or stick them in subdirectories yourself.
[/quote]
I also remember VC will put all .obj files to one directory, so the source files with same names will override each other's .obj file, no matter which directory the source files are in.
Anyway, I always use unique source file name to avoid trouble.

Here also has some discussion on that issue,
[url="http://stackoverflow.com/questions/1968685/visual-c-2008-express-cpp-filename-conflict"]http://stackoverflow...lename-conflict[/url]
And here,
http://connect.microsoft.com/VisualStudio/feedback/details/361228/visual-c-compilers-only-one-file-of-two-with-the-same-name Edited by wqking

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this