Sign in to follow this  
Waterlimon

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

Recommended Posts

I have a header file like

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

another header like

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

and a cpp like

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

and the main cpp

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


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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1318087583' post='4870511']
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?
[/quote]

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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.
[code]
extern Mahclass<1, 2> Array[1024]; // in a header file

Mahclass<1, 2> Array [1024]; // in one source file
[/code]

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