• Advertisement
Sign in to follow this  

class question

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I have a class declared as an extern, and when i try to use it in my code , i get : [Linker error] undefined reference to `Logger' . Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
There's no such thing as an extern class. Assuming that you meant that have declared a global variable of a class type as extern, then in one source file you still need to define the variable. Ex:

extern Foo foo; // in the header

Foo foo; // in one source file at namespace scope (not inside a class or function)

Share this post


Link to post
Share on other sites
Quote:
Original post by Unconquerable
Yeah thats what i ment.


then post the code where you extern it as well as the code where you define it.

-me

Share this post


Link to post
Share on other sites

class Log
{
private:
ofstream outFile;
public:
Log( string fileName )
{
outFile.open( fileName.c_str() );
}


void LogInt ( int number)
{
outFile << number;
}
~Log()
{
outFile.close();
}
};



extern Log Logger;

....

Logger.LogInt(iShiptwo_x);




Share this post


Link to post
Share on other sites
It looks like you have a constructor that accepts a file name as a string, but you don't appear to supply it a filename, and it doesn't have a default.

EDIT: Take everything I say with a grain of salt, I am still in C++ learning phase as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by -JetSirus-
It looks like you have a constructor that accepts a file name as a string, but you don't appear to supply it a filename, and it doesn't have a default.

EDIT: Take everything I say with a grain of salt, I am still in C++ learning phase as well.


I fixed that, still the same error though.

Share this post


Link to post
Share on other sites
Yeah, in one of your source files then, you need to instantiate the object.

I.e.

foo.h:

extern CFoo g_foo;

main.cpp

CFoo g_foo;

g_foo.bar();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement