Need a quick fix to this problem..

Started by
12 comments, last by TFS_Waldo 17 years, 9 months ago
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.
enum Bool { True, False, FileNotFound };
Advertisement
Quote:Original post by ToohrVyk
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.


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++?
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.
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. =)
:==-_ Why don't the voices just leave me alone?! _-==:

This topic is closed to new replies.

Advertisement