whats wrong here?
anyone,
Please look at this simple class declaration and tell me what stupid thing I am doing wrong, but first here are the errors I am getting when I do a rebuild( Im using vc++ 6.0).
Linking...
"private: static int Jewel::height" (?height@Jewel@@0HA) already defined in jewelstring.obj
"private: static int Jewel::width" (?width@Jewel@@0HA) already defined in jewelstring.obj
Debug/jewelTest.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Here is the class:
#ifndef _JEWEL_H_
#define _JEWEL_H_
class Jewel
{
public:
//set value to default
Jewel() : value(0)
{
height = width = 0;
}
Jewel(int val, int Width, int Height)
{
value = val;
width = Width;
height = Height;
}
inline void setValue(int val) { value = val; }
inline void setWidth(int val) { width = val; }
inline void setHeight(int val){ height = val;}
inline int getWidth() { return width; }
inline int getHeight(){ return height;}
inline int getValue() { return value; }
private:
int value;
static int width; //each jewel has
static int height; //same dimensions
};
//now allocate the storage for the statics
int Jewel::width = 0;
int Jewel::height = 0;
#endif
//end of file
I know the static modifier is causing the problem, but I dont know why. Also, there is absolutely no data member, function, or otherwise in jewelstring.h that is named or references the variables width or height.
thanks in advance,
skitzo
Edited by - skitzo_smurf on 7/4/00 4:41:34 AM
I''ve never had good luck with static variables in classes. You could switch to global variables since that''s basically what they are. Maybe you can use the extern keyword since that''s used to fix this problem for global variables.
Also if the width is the same for all jewels and its know at compile time you should just use a define.
Also if the width is the same for all jewels and its know at compile time you should just use a define.
ugH! THanks for replying. Yeah I could use globals OR get rid of the static keyword(in fact I did both and it worked fine) but its the principle of the thing! I guess im just one stubborn guy. I''ve spent a lot of late nights screaming at the compiler just because I want to do things the right way instead of the easy way(Havent we all?). Oh well.
Thanks for the help,
skitzo
Thanks for the help,
skitzo
quote:Original post by skitzo_smurf
//now allocate the storage for the statics
int Jewel::width = 0;
int Jewel::height = 0;
Is that in your .h file? If so, you need to move it to a .cpp file, probably at the beginning of your class implementation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement