# Need a quick fix to this problem..

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

##### Share on other sites
Quote:
 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.

That's just wrong. The compiler doesn't care if the function is inline or out-of-line in the .cpp file -- in fact, the compiler just compiles a stream of code; the #include <> parts are replaced by the actual file contents by the pre-processor before the compiler even sees the text.

As for the original problem: it looks almost as if you're trying to compile as C source, instead of compiling as C++ source. Check the file name, or check the compiler options.

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

It's working now but thnx anyways..

Now I have a new problem..

I'm not used to C++ being a Java man and all, so now I have two arrays declared and one is full of HiScore structs filled with data (HiScores). The other (SortedScores) is used to store data from the first like this:

while(swap == true)
{
swap = false;
for(int i = 0; i < lastScore; i++)
{
tester = HiScores.score;
testee = HiScores[i+1].score;
if(tester > testee)
{
SortedScores = HiScores;
}else
{
SortedScores = HiScores[i+1];
swap = true;
}
} // end for
} // end while

So now this class member wants to assign the newly SortedScores into the primary class attr HiScores.. I did it this way and the compiler didn't like it:

HiScores = SortedScores;

This gave me a "error C2106: '=' : left operand must be 1-value" error msg..

Any ideas how this should be done in C++?

##### Share on other sites
You cannot assign arrays to each other. Try something like this instead:

#include <cstring>const char *names[] = {"jim","jack","john","james","jerry","jules","jane","fred","wilma","stephen","Philip" };struct HiScore{    char name[12];    int score;        HiScore()    {        strcpy( name,names[rand() % 10] );        score = rand() % 100;    }};struct ScoreComparator{    bool operator()( const HiScore &one, const HiScore &two )    {       if( one.score == two.score )       {           return (strcmp( one.name, two.name ) < 0);       }       return (one.score > two.score);    }};const int NUM_HISCORES = 10;class Settings{   HiScore Hiscores[NUM_HISCORES];public:   void sortScores()   {       std::sort( Hiscores , Hiscores + NUM_HISCORES, ScoreComparator() );   }   void printScores()   {       for( int i = 0 ; i < NUM_HISCORES ; ++i )        {            std::cout << i << ") " << Hiscores.name << " score " << Hiscores.score << std::endl;         }   }};int main( int argc, char **argv ){    Settings settings;    settings.sortScores();    settings.printScores();}

Quick hacked example, buts thats sorting in c++.

Hope vc 6 is up to it :/

Comsider also the use of std::string for string operators, similar to java.lang.String in some ways.

##### Share on other sites
I think this would be one of the only times I have not seen SOMEONE mention something about using 'char' arrays, instead of std::string. =)