Jump to content
  • Advertisement
Sign in to follow this  
XTAL256

problem with 'static' in class

This topic is 3759 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

This may sound like a n00bish question but i get "LNK2001: unresolved external symbol" errors in MSVC when i use static variables in classes. Here is some of my code:
class Button : public Component {
public:
    // Images used for all objects of this component.
    static Image *upImg, *downImg, *hoverImg;
    static int divLeft, divRight;
    static void loadImages();

    GLuint dlUp, dlHover, dlDown;  // OpenGL display lists

	Button() {}
    Button(String text, int x, int y, int w = 150, int h = 50);

    void draw();
	void update();
};

and i get these erros:
ComboBox.obj : error LNK2001: unresolved external symbol "public: static int gui::ComboBox::divBoxTop" (?divBoxTop@ComboBox@gui@@2HA)
ComboBox.obj : error LNK2001: unresolved external symbol "public: static int gui::ComboBox::divSelRight" (...)
ComboBox.obj : error LNK2001: unresolved external symbol "public: static class allegx::Image * gui::ComboBox::upImg" (...)
ComboBox.obj : error LNK2001: unresolved external symbol "public: static class allegx::Image * gui::ComboBox::downImg"
... etc.
any help? thanks

Share this post


Link to post
Share on other sites
Advertisement
When you declare a static variable in a class, you still need to allocate storage for it. In your case you'd need to put:

int Button::divLeft;
int Button::divRight;
// etc.

in one source file at namespace scope (not inside another class or a function).

Share this post


Link to post
Share on other sites
.cpp file

Image* Button::upImg = 0;
Image* Button::downImg = 0;
Image* Button::hoverImg = 0;

int Button::divLeft = 0;
int Button::divRight = 0;

Share this post


Link to post
Share on other sites
Ok, thanks. What about static functions? My graphics functions such as drawImage are not static (and not in a class) but i get link errors. If i make them static (declaration not definition) i get:

C:\...\VC\include\xlocnum(235) : error C2129: static function 'void allegx::drawImage(allegx::Image *,int,int,int,int,int,int,int,int)' declared but not defined
c:\...\allegx\Graphics.h(30) : see declaration of 'allegx::drawImage'

Share this post


Link to post
Share on other sites
You need to be linking to whichever lib has the drawImage function in it. If you can post the code that produces that error maybe we can get a better feel for the problem.

Share this post


Link to post
Share on other sites
Using my super psychic powers assisted by a magic eight ball I've determined that the link error you were getting was LNK1285: Redundant cheese error. This is commonly caused by ordering a cheese lover's pizza with double extra cheese and leaving a slice overnight on the plate on the computer case. I suggest washing the case gently with soapy water and drying with a soft cloth. Also consider not using your case as a convenient flat surface.

If this isn't the problem you might want to post the actual error and maybe the code you were having problems with.

Share this post


Link to post
Share on other sites
Alright, Alright, there's no need for sarcasm :) I assumed the problem was similar to my first, i'm used to programming in Java where static (or any) declarations and definitions are not separate i.e. Java doesn't have header files.
My graphics functions are not in a different lib, they are just in another source file. I create a display list for each GUI component:

// in Button class
createComponentDL(dlUp, upImg, x, y, width, height);
...
// in gui.h which is included with .cpp files
inline void createComponentDL(GLuint &id, Image* img, int x, int y,
int width, int height, int divLeft, int divRight) {
beginList(id);
drawImage(img, x, y, ...);
endList();
}


And my graphics code:

// Graphics.h
void drawImage(Image* img, int x, int y);
etc.

// Graphics.cpp
void drawImage(Image* img, int x, int y) {
...
}
etc.


refer to my previous post for my question. please ask if you have any more questions about my code.

Share this post


Link to post
Share on other sites
And the actual linker error? Unless, of course, it was actually a redundant cheese error, in which case follow the above suggestion.

Share this post


Link to post
Share on other sites
like i said above, see my previous post. The error was:
C:\...\VC\include\xlocnum(235) : error C2129: static function 'void allegx::drawImage(allegx::Image *,...)' declared but not defined

Ok, well that's what i got when i made 'drawImage' static. If i don't have it static i get unresolved symbol error.

Share this post


Link to post
Share on other sites
You aren't showing us something... the error you pasted clearly shows it defined as allegx::drawImage. The snippet you posted for the declaration and definition only show drawImage. It would appear--again, without enough information--that your declaration in the header has the function in the allegx namespace, but the definition doesn't.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!