Skip to main content
gamedev.net
🔒 Locked

Static and C++

Started by Codejoy 5 replies 550+ views
Original Post
Codejoy
Codejoy
So I am trying to port some code from OpenGL written in C++ to another API keeping the C++ ness of it? I have a class:
class CDrawBlock {

public:      
        static void DrawBlocks();
        static void DrawBlock(blah,blah,blah);
private:
        static NSprite blockSprites;
};
I then say from another class:
void GamePlay::idlePlay()
{
  ...
  ...
  ...

  CDrawBlock::DrawBlocks();
}
This works fine up until from my main loop i say:
mainLoop()
{
       GamePlay::idlePlay();
}
then i get a linker error: Linker Error: Undefined : "CDrawBlock::blockSprites" Referenced from "CDrawBlock::DrawBlocks" in DrawBlocks.cpp I can get rid of this linker error by removing the static keyword from the drawblocks files...but then I get compile errors saying something bout illegal use of a non-static member. So ya, I am stuck....I am guess its the way I cam calling the functions using the :: i.e.:
mainloop()
{
   GamePlay::idelPlay();//if i dont make this call it compiles fine.

}
but this is how the code I am porting does it (am I wrong to assume that the :: makes it where you have no need to instantiate your objects beforehand?) Thanks for any help here... The less code I have to change the better. Shane
crazy166
crazy166
You need a line similar to the following in a .cpp file:

NSprite CDrawBlock::blockSprites;

If the NSprite class needs a constructor you can do:

NSprite CDrawBlock::blockSprites = NSprite(blah,blah,blah);

The NSprite constructor will probably be called before any of your game init functions though, so be careful.

Codejoy
Codejoy
Hmm that line gave me an illegal access using declaration compile error.

Oddly enough, I am also wanting to ask more of a C++ guru than myself, why was the code im porting using the stuff like GamePlay::idlePlay(); instead of:

GamePlay myGame;
myGame.idlePlay();

?? Is the method calling with :: just shorthand for negating the need of a class object and instantiation?

Thanks,
Shane
ahhh okay i see where to do the
NSprite CDrawBlocks::blockSprites;, It hought i had to do that in the .h file, but instead i have to do it in the .cpp and looking through the other files in the code im porting, its done the same way for the members of those classes...so thanks i didnt notice it earlier.

-Shane





[edited by - Codejoy on March 9, 2004 1:35:54 PM]
Zahlman
Zahlman
Class::method() is the syntax for invoking a static method of that class. These methods basically "belong to the class" rather than to instances; instance methods can invoke these methods() without the Class:: prefix. It''s basically like a namespace. (At least, that''s how it works in Java - where it''s basically your only option for namespaces, although the packaging system helps you out there.) As a result, they don''t have _thiscall calling conventions; you can''t use "this" in them because there''s no object to which "this" might refer. (On the flip side, this allows you to use function pointers to these methods from contexts where a "this" could not be supplied. This is the part where someone more knowledgable than I copies-and-pastes a standard ranting about "bound member function pointers" or something like that.)

Your problem has nothing to do with the static-ness of the variable (when you take out the ''static'' keyword, you replace a linker error with a compiler error, which is understandable because you''re trying to call a now-non-static method without an object) and everything to do with how you set up your linking. Unfortunately, I don''t know how to fix it. Something to do with putting the right #include/extern/etc. magic in the right places. Icky stuff. Use Java instead.
Codejoy
Codejoy
Ya I assumed all this was having to do with icky stuff I dont like to deal with (as I havent dealt with it before) so I just trying to understand why the original author wrote it that way, instead of just doing the whole

GamePlay myPlay;
myPlay.idlePlay();
etc...

Sounds like the only reason is to do function pointer calls?

hmmm verrryyy interesting.

Shane
Henners
Henners
My 2 cents:
Where have you initialized the static member variable of your CDrawBlock class? The NSprite object must be initialized - even if it has a default constructor it must be created at some point.
Whether your static variable is private protected or public, you must do something like:

NSprite CDrawBlock::blockSprites;

So the static member functions can be called without instantiating an object anywhere at all, but in order to use static member variables, they need to be initialized in the class definition. Because your static member functions use static member variables that have not been referenced previously in your code, they will fail.

Hope that made sense, and helps a bit.

Henners

Didn't see crazy166's reply - obviously I agree. Make sure you initialize the static member variable in the class definition, or elsewhere in global scope. Basically, you can't declare it in your mainLoop or anything - it can't be local scope.

[edited by - Henners on March 9, 2004 2:50:00 PM]
---------------------------------Use Firefox, you'll never ever look back.
SpaceDude
SpaceDude
quote:
Original post by Codejoy
I can get rid of this linker error by removing the static keyword from the drawblocks files...but then I get compile errors saying something bout illegal use of a non-static member.



The reason your linker error dissapears is because if there is an error in the compile it never gets to the linking stage... So even if the link error still exists, it won''t tell you until your compile actually works...

You can test that out just by introducing any random compile error and your link error will magically dissapear.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.