# extern problem

This topic is 4677 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.

##### Share on other sites
Thanks
I don't want singletons, i'm doing it just for myself so i will know which class should have only one instance. Maybe if everything will work ok i'll implement them.

I was trying to obtain a global pointer to a class, not global instance of a class (but maybe i'll change my mind). If i understood right i should put "input = new CInput();" into Input.cpp, but then how will i know when it's created i.e. before or after my logging class which will also have global pointer and will be created in the same way?

##### Share on other sites
simple solution: create InitLog and InitInput functions that simply creates the objects and assigns the pointers into the correct units then in main simply go:

InitLog();
InitInput();

simple works get the job done.

##### Share on other sites
If you just what a "quick hack" so that your current code will run, get rid off

CInput* input = new CInput... in windows.cpp

and alter the file to:

CInput* input;   // This is where CInput* input is actually stored                 // and is why your linker was previously grumblingvoid InitInput()   // or where ever you initialise CInput{   ...   input = new CInput   ...}

##### Share on other sites
The Gecko is correct. You need to have all declarations in your *.h and all your definitions in your *.cpp. Another thing you could implement to make your life easier are namespaces:
// input.h#ifndef _INPUT_H_#define _INPUT_H_namespace InputModule {    class CInput    {        void SomeFunction();    };    extern CInput *input; // declaration    void Initialize(); // declaration}#endif// input.cpp#include "input.h"namespace InputModule {    CInput *input; // definition    void Initialize()    {       input = new CInpput();    }    void CInput::SomeFunction(){ // definition        // code    }}`

This will help you handle the global variables in different modules. You just have to use InputModule::Initialize() to initialize the input module.

##### Share on other sites
Quote:
 Original post by DigitalDelusionI see a definition declaration but no declaration definition. Where exactly did you put the declaration definition of that pointer? it has to reside in some sourcefile somehwere you know :)
Correction, but ya, you need a definition somewhere in a .cpp file.

##### Share on other sites
Quote:
Original post by Roboguy
Quote:
 Original post by DigitalDelusionI see a definition declaration but no declaration definition. Where exactly did you put the declaration definition of that pointer? it has to reside in some sourcefile somehwere you know :)
Correction, but ya, you need a definition somewhere in a .cpp file.

Geez, I always tend to mixup thoose two :) rating cookie for you sir.