# Need a quick fix to this problem..

This topic is 4491 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
Maybe you need to add an extra ":" in
Quote:
 HiScore Settings:GetScore(int index)

##### 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 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 on other sites
I doubt it's a VS 6 compiler issue. Maybe post your code so we can check it out.

Matt

##### Share on other sites
Quote:
 Original post by hplus0603The 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

##### 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 on other sites
Ok I managed to get it to work now!!

Thanx soooo much everyone!!!!!

##### 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 on other sites
Quote:
 Original post by ToohrVykMushu: 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

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×