• 13
• 27
• 9
• 9
• 20

extern problem

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

Recommended Posts

Hi! This was probably asked many times before but i really can't find a solution. I have few classes declared in separate header files like this:
#ifndef _INPUT_H_
#define _INPUT_H_

class CInput
{
blabla;
};

extern CInput *input;

#endif


In Windows.cpp file where i got WinMain function i include Input.h and do CInput *input = new CInput(); But when linking, wherever i'm using input pointer (even in Windows.cpp) i get error: [Linker error] undefined reference to input'(i'm using Dev-cpp). What's wrong?

Share on other sites
How are you using the pointer input after you have declared it?

Are you still using the *?

Also, why are you declaring it as extern?

Share on other sites
What does 'nm Windows.o' output (at least for the input symbol) ?
What does nm output for the other object files ?

Share on other sites
I see a definition but no declaration. Where exactly did you put the declaration of that pointer? it has to reside in some sourcefile somehwere you know :)

Share on other sites
The actual instance of CInput needs to go inside the "input.cpp" file

General rules: always declare things in header files (i.e. use extern) never
place instances in header files. The instances go in the corresponding source file (*.cpp)

Basically, in your case its poor form to declare
 extern CInput* input`

within "input.h" and then make an instance of it within "windows.cpp".

Also, inside windows.cpp you have re-declared a private "version" of CInput

i.e. Change

CInput* input = new CInput(...)

to

input = new CInput(...)

--------------------------------------------------------------------

"proper" way of doing it:

//--------------------
// input.h:

class CInput{
....
};

extern CInput input; // no pointer

//---------------------
// input.cpp:

#include "input.h"

CInput input;

Share on other sites
Quote:
 Original post by TheFatGeckoForgot to say, have a google about good c/c++ style and header files and singletons

It's not poor style, and putting the instance at static global scope could be impossible sure it's not a good idea to litter globals like that but it could be a nice way to get things done and heck you can't say for sure that that object could be constructed like that and your new line is just wrong.

About the only thing you got right was how he should declare the variable being defined in a source file.

Share on other sites
DigitalDelusion

I don't get your critism. What's not poor style? I clearly stated that
placing a declaration in one header file and the instance in another is poor form. End of story. Whether or not the instance is a static or needs to be initialised by some class within the "input.cpp" file is up to guy writing.

Share on other sites
Look over what you posted.
You don't have any better style you made the (probably false) assumption that CInput could be constructed without preconditions and you botched the code.

Yes his design should probably be reworked to better hide the details either via a singleton a procedural interface or by simply passing around the CInput pointer without making it global.

So you didn't solve his problem and posted erranous code claiming it to be better, that's hardly helpful in my eyes.