Unres. ext. symbol with function decl. in header and definition in cpp

Started by
9 comments, last by alvaro 12 years, 6 months ago
I have a header file like


//A.h
namespace header
{
void func();
}


another header like


//B.h
namespace headers
{
#include "A.h"
}


and a cpp like


//C.cpp
#include "A.h"
void header::func()
{
DoStuff();
}


and the main cpp


//main.cpp
#include "B.h"
int TheFancyWindowsMainFunction()
{
headers::header::func();
}



My problem is that for some reason i get the "Unresolved external symbol blah blah referenced in function TheFancyWindowsandsoon" error.

Is there something wrong with the way ive set up the headers and all?

Both of the headers have inclusion guards.

Ill post the code if you cant see anything wrong with that... I tried throwing around some keywords and changing the places of stuff, but the function is only found if i define it in the header.

Oh and the cpp's are in the Source files directory and the headers in the Header files one. (MSVC++ 2010 express)

o3o

Advertisement
You've declared the function headers::header::func(), but defined the function as header::func(). So when the linker goes looking for headers::header::func(), it doesn't find it. Avoid #including file inside of namespaces, the resulting aliasing mess is rarely worth it.
You shouldn't include a header file within a namespace block: It will confuse the hell out of everybody, including yourself. Why were you trying to do that?

You shouldn't include a header file within a namespace block: It will confuse the hell out of everybody, including yourself. Why were you trying to do that?


I wanted the stuff in the header inside a namespace with a bunch of other headers... Gotta figure out a cleaner way to do it...

o3o

Another question:
How exactly do i declare an array in a header without making it complain about multiple versions found OR unresolved external symbol?
I tried making it static and const and extern and stuff but cant figure it out...
Cannot put it in the class because the class is a template and it would make multiple copies.

o3o

Show us your attempt at an array declaration.
Mahclass<1,2> Array[1024];

o3o

Global arrays work just like any other global variable. In the header you create an extern declaration. In one source file you place the definition.

extern Mahclass<1, 2> Array[1024]; // in a header file

Mahclass<1, 2> Array [1024]; // in one source file
I tried that but then it complains about unresolved....

Does it matter that its in a namespace?

o3o

The definition should occur in the same namespace as the declaration.

This topic is closed to new replies.

Advertisement