Jump to content
  • Advertisement
Sign in to follow this  
ArchangelMorph

Need a quick fix to this problem..

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

In the Settings class for my game I defined my Hiscores array as follows: struct HiScore { char name[12]; int score; }; class Settings { HiScore Hiscores[20]; : . Now maybe I don't understand structs and arrays in C++ too well but I tried to use it in my class member like this: HiScore Settings:GetScore(int index) { return Hiscores[index]; } And it doesn't like it.. Comes up with a compile error "Hiscores : undeclared indentifier" and "subscript requires array or pointer type" Any help you can give me will be much appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Maybe you need to add an extra ":" in
Quote:
HiScore Settings:GetScore(int index)

Share this post


Link to post
Share on other sites
The code you posted shows no reason why you shouldn't be able to compile that.

In fact, this code should compile just fine, if you paste it all into a single file:


struct HiScore
{
char name[12];
int score;
};

class Settings
{
HiScore Hiscores[20];
public:
HiScore GetScore(int index);
};

HiScore Settings::GetScore(int index)
{
return Hiscores[index];
}



Note that you only had one colon in the Settings::GetScore() definition, though -- if that's in your source, too, then you want to fix that first :-)

Share this post


Link to post
Share on other sites
sorry the colon was a typo, it's not in my source..

I checked through and there's no difference between what you put and what I put..

I don't understand.. maybe its a VS 6.0 issue? but if so then what could it be??

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
The code you posted shows no reason why you shouldn't be able to compile that.

In fact, this code should compile just fine, if you paste it all into a single file:


struct HiScore
{
char name[12];
int score;
};

class Settings
{
HiScore Hiscores[20];
public:
HiScore GetScore(int index);
};

HiScore Settings::GetScore(int index)
{
return Hiscores[index];
}



Note that you only had one colon in the Settings::GetScore() definition, though -- if that's in your source, too, then you want to fix that first :-)


Ok i'm very confused!

I took this body of code you gave me and attempted to compile it as a seperate, stand-alone .cpp file in its own project and low and behold it STILL didn't work!

I get errors of the nature:

"error C2236: unexpected 'class' 'Settings' "
"error C2143: syntax error : missing ';' before '}' "
"error C2447: missing function header (old-style formal list?) "
"error C2653: 'Settings' : is not a class or namespace name "
"error C2065: 'HiScores' : undeclared identifier "
"error C2109: subscript requires array or pointer type "
"error C2440: 'return' : cannot convert from 'int' to 'struct Hiscore'
No constructor could take the source type, or constructor overload resolution was ambiguous

Please help if you can, I really need to overcome this problem..

Share this post


Link to post
Share on other sites
If you just included that block in a .cpp file it should be, instead -

struct HiScore
{
char name[12];
int score;
};

class Settings
{
HiScore Hiscores[20];
public:
HiScore GetScore(int index)
{
return Hiscores[index];
}
};



The reason for this is that when you define a class locally (within a *.cpp) you have to define the functions within the class. You can't just declare them like you would in a header (in VC6, at least). hp's original code would work fine if you put the class definitions into a header, and the function definitions into a cpp.

The above code compiles in VC6.

Share this post


Link to post
Share on other sites
Can you post the full source file, along with the full error messages (including, for instance, line numbers)?

Mushu: to the compiler, all files are translation units, there is no difference between .cpp and .h and .foo files.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Mushu: to the compiler, all files are translation units, there is no difference between .cpp and .h and .foo files.

VC6 doesn't exactly conform to those nice standards, and, unfortunately, a lot of people still use VC6.

error C2599: 'GetScore' : local class member functions must be defined within the class see declaration of 'GetScore'
error C2601: 'GetScore' : local function definitions are illegal


:P

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!