class question
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.
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)
extern Foo foo; // in the header
Foo foo; // in one source file at namespace scope (not inside a class or function)
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
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);
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.
EDIT: Take everything I say with a grain of salt, I am still in C++ learning phase as well.
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.
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();
I.e.
foo.h:
extern CFoo g_foo;
main.cpp
CFoo g_foo;
g_foo.bar();
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement