Need a quick fix to this problem..

Started by
12 comments, last by TFS_Waldo 17 years, 10 months ago
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!
Advertisement
Maybe you need to add an extra ":" in
Quote:HiScore Settings:GetScore(int index)
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 :-)
enum Bool { True, False, FileNotFound };
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??
I doubt it's a VS 6 compiler issue. Maybe post your code so we can check it out.

Matt
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
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..

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.
Ok I managed to get it to work now!!

Thanx soooo much everyone!!!!!

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.
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

This topic is closed to new replies.

Advertisement