Sign in to follow this  
pavel989

c++ static vars in a class

Recommended Posts

hey guys, I have a class with a buncha static variables. compiling my program i get a buncha linker errors that follow this pattern: 1>window.obj : error LNK2001: unresolved external symbol "private: static struct tagPOINT * Window::lpPoint" (?lpPoint@Window@@0PAUtagPOINT@@A) i think im gettin them bc i have another header file, which is holding a global object of that class. it links to the header of the class header. and thus, all the files that link to my object header, are screwing up the class, in a sense. Is that possible? i really dunno what to do.

Share this post


Link to post
Share on other sites
When you declare a static variable in a class, you need to add a definition of the variable somewhere; generally in the same file that you implement the rest of the class, though this isn't necessary. For example if you have:

class MyClass {
public:
static int i;
};

In one source file at namespace scope (not inside a function or class definition) put:

int MyClass::i;

Share this post


Link to post
Share on other sites
If you have a class with a static member variable, in atleast one .cpp file (ie where the rest of the class definition is) you need to actually define the variable at the global scope (or inside whatever namespace your "Window" class exists in).

something like:
in window.cpp
struct tagPOINT * Window::lpPoint = NULL;


:( SiCrane is too fast.

Share this post


Link to post
Share on other sites
tried that, but dint work.

ima check maybe i did something wrong. i felt that it worked like an extern where i have to declare it.

does it matter that i haven't implemented namespaces

Share this post


Link to post
Share on other sites
actually i think i did bad linking:




class file
|
object header-----------|
______________________________|
| | |
file1 file2 file3

so technically its being called like 3 times or w/e. Could that be the problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by pavel989
so technically its being called like 3 times or w/e. Could that be the problem?

No, if you have proper ifdef guards (your error message indicates you have not such problem).


Instead of:
// Window.h
struct tagPOINT;
class Window
{
static tagPOINT* lpPoint;
};
// Window.cpp
tagPOINT* Window::lpPoint = NULL;

you could try with:

// Window.h
struct tagPOINT;
class Window
{
static tagPOINT*& lpPoint() {
static tagPOINT* point = NULL;
return point;
};
};
// Window.cpp empty!

You need to replace lpPoint with lpPoint() everywhere, but it might solve your problem.

Share this post


Link to post
Share on other sites
No, it does not make sense. It is a bad solution.
SiCrane and KulSeran have both answered the question.

Adding a static member variable in a class body only provides a declaration for that variable. You still need to provide one, and only one, definition. Not extern. Not a pseudo-singleton. Just a definition in a source file.

You shouldn't define a global variable in a header file anyway, since it usually leads to multiple definitions.

Incidentally, C++ does not have tag names, so there is no point in doing "struct tagFoo", which is a C-ism.

Share this post


Link to post
Share on other sites
Quote:
Original post by pavel989
tried that, but dint work.

ima check maybe i did something wrong. i felt that it worked like an extern where i have to declare it.

does it matter that i haven't implemented namespaces


No. As static data members have class scope then ensuring a valid class definition is sufficient.

Static data members must be declared at class scope and defined at file scope (as stated previously).

One quick point... static data members are subject to class-member access rules. As you've declared lpPoint to be a private static member only class-methods and friends have access to it.

Anyway... because you're dealing with a pointer to a structure I'd personally ensure that it was a well defined pointer type prior to its usage in any data definition. For example:


// somewhere.h

typedef struct tagPOINT
{
...
} TAGPOINT, *P_TAGPOINT;

//window.h

#include "somewhere.h"
class Window
{
private:
static P_TAGPOINT lpPoint;
};

//window.cpp

#include "window.h"

P_TAGPOINT Window::lpPoint;





The issue you would have would be assigning a valid address to this pointer when it's used as a private static member of Window. You would need to provide a public static interface to do this. Within Window add:


public:
static void SetTagPoint(const P_TAGPOINT pPt)
{
lpPoint = pPt;
}


Cheers,

Timkin

Share this post


Link to post
Share on other sites
well i really did not want to make this a global variable for that reason. but i didn't find a way around it. The thing is, it's just a single class, which has data about the current window. like its position and size, and hdc and so forth. an lppoint and all the rest are win32 things. So, i have a bunch of code that needs to access the window data so i tried to make it a global variable, which clearly gave me problems.


Well just now i found that the problem i was having is that i was initializing its value in the wrong place. I understood that if i define a static variable i must also declare its value. i thought i was doing so, but i was doing it in the class header file, which for some reason was causing problems. moved em down to the class cpp file and now its all good.

thanks to everyone that helped.

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