tags:

views:

2836

answers:

6

Here's the situation:

I just started my first C++ class. I know nothing about C++ and despite this being the an intro to C++ class, the teacher has taught pretty much NO syntax for the language, just logic that we already know.

Now he gave us a program description and said, "write it, lol." I understand the logic fine, but as I said before, I know nothing about C++. Thus I wrote it first in java (which I do know), and then once I got it working in java, I tried to code it over to C++.

However now I am getting the following error when I compile

uxb3% g++ -o Race race.cc
Undefined                       first referenced
 symbol                             in file
main                                /usr/local/gcc-4.1.1/bin/../lib/gcc/sparc-sun-solaris2.10/4.1.1/crt1.o
ld: fatal: Symbol referencing errors. No output written to Race
collect2: ld returned 1 exit status

Can someone please help me with this?

Here is my code in a .txt file: http://rapidshare.com/files/195742284/race.txt.html

and here it is in a copy paste:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;
class Race
{

        void main()
        {
                executeRace();
        }

        int randomMove()
        {
                srand(time(NULL));
                int randomInt = rand() % 100 + 1;
                return randomInt;
        }

        void executeRace()
        {
                int rabbitPosition = 1;
                int turtlePosition = 1;

                cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

                while (rabbitPosition <=70 && turtlePosition <=70)
                {
                        printPositions(rabbitPosition, turtlePosition);

                        turtlePosition = turtleMoveSquares(turtlePosition);
                        rabbitPosition = rabbitMoveSquares(rabbitPosition);
                }

                printWinner(rabbitPosition, turtlePosition);

                tie(rabbitPosition, turtlePosition);
        }

        int turtleMoveSquares(int tPosition)
        {

                int turtleMove = randomMove();

                if(turtleMove >=1 && turtleMove <= 40)
                        tPosition = tPosition + 4;

                if(turtleMove >= 41 && turtleMove <= 50 )
                        tPosition = tPosition - 2;

                if(turtleMove >=51 && turtleMove <=100)
                        tPosition = tPosition + 2;

                if(tPosition < 1)
                        tPosition = 1;

                return tPosition;
        }


        int rabbitMoveSquares(int rabbitPosition)
        {

                int rabbitMove = randomMove();

                if(rabbitMove >=1 && rabbitMove <= 25)
                        rabbitPosition = rabbitPosition;

                if(rabbitMove >=26 && rabbitMove <= 55)
                        rabbitPosition = rabbitPosition + 10;

                if(rabbitMove >=56 && rabbitMove <=60)
                        rabbitPosition = rabbitPosition - 15;

                if(rabbitMove >=61 && rabbitMove <= 90)
                        rabbitPosition = rabbitPosition + 5;

                if(rabbitMove >=90 && rabbitMove <=100)
                        rabbitPosition = rabbitPosition - 3;

                if(rabbitPosition < 1)
                        rabbitPosition = 1;

                return rabbitPosition;
        }

        void printPositions(int rabbitPositions, int turtlePositions)
        {
                int turtleCount;
                int rabbitCount;
                int endCount;

                if(rabbitPositions == turtlePositions && rabbitPositions != 1)
                {
                        turtleCount = 1;

                        while(turtleCount < turtlePositions)
                        {
                                cout << "-";
                                turtleCount = turtleCount+1;
                        }
                        cout << "OUCH!";
                }

                else
                {
                        turtleCount = 1;
                        rabbitCount = 1;
                        endCount=1;

                        if(turtlePositions < rabbitPositions)
                        {
                                while(turtleCount < turtlePositions)
                                {
                                        cout <<  "-";
                                                turtleCount = turtleCount+1;
                                }

                                cout << "T";

                                while(rabbitCount < (rabbitPositions - turtlePositions))
                                {
                                        cout <<  "-";
                                        rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                        }

                        if(rabbitPositions < turtlePositions)
                        {
                                while(rabbitCount < rabbitPositions)
                                {
                                        cout << "-";
                                                rabbitCount = rabbitCount+1;
                                }
                                cout << "H";

                                while(turtleCount < (turtlePositions - rabbitPositions))
                                {
                                        cout << "-";
                                        turtleCount = turtleCount+1;
                                }
                                cout << "T";

                                cout << "\n";
                        }
                }
        }

        void printWinner(int rabbitPosition, int turtlePosition)
        {
                if(turtlePosition >= 70 && rabbitPosition < 70)
                {
                        cout << "TORTOISE WINS!!! YAY!!!\n";
                }
                else if(rabbitPosition >=70 && turtlePosition < 70)
                {
                        cout << "Hare wins. Yuch.\n";
                }
                else if(rabbitPosition >=70 && turtlePosition >=70)
                {
                        cout << "It's a tie\n";
                }
        }

        void tie(int turtlePosition, int rabbitPosition)
        {
                if(rabbitPosition >=70 && turtlePosition >=70)
                        executeRace();
        }
};
+13  A: 

It's complaining about main being undefined, and it's right. C and C++ require a standalone function named main which returns int. It is not sufficient to have a class with a method named main; the compiler only cares about the standalone one.

Another thing to remember is that the default visibility for members in a class type is private. Either change your class to use struct — that's pretty much the only difference between the two — or specify public visibility:

class Race {
public:
  void main() { ... }
};

Then you can call the main function from your class:

int main() {
  Race race;
  race.main();
  return EXIT_SUCCESS;
}
Rob Kennedy
Nice catch on the default access level.
Bernard
+3  A: 

You need a main() function to give the program a starting point:

int main()
{
     // whatever...
     // Maybe:

     Race race;

     race.main();
}

But it would probably make sense to have many of your methods be static methods since they don't rely on any instance members of the Race class.

Michael Burr
If you're going to use static methods, I would tend to just use functions. Perhaps in a namespace.
Bernard
You'll get no argument from me.
Michael Burr
+3  A: 

You need a main function:

// The arguments are only needed if passing arguments to your program.
// You could just use `int main()`.
int main(int argc, char** argv)
{
    Race race;
    race.executeRace();
}

or so, without seeing your specific error.

Bernard
+3  A: 

C++ is really nothing like Java. For example, functions don't have to be part of a class. You need to move the function main() so that it is outside the class.

class Race {
  // stuff
};

int main() {
  // stuff
}
anon
+2  A: 

Perhaps this is what you meant

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;



class Race
{

public:
    void ExecuteRace()
    {
        int rabbitPosition = 1;
        int turtlePosition = 1;

        cout << "BANG!!!" << endl << "AND THEY'RE OFF!!!";

        while (rabbitPosition <=70 && turtlePosition <=70)
        {
            printPositions(rabbitPosition, turtlePosition);

            turtlePosition = turtleMoveSquares(turtlePosition);
            rabbitPosition = rabbitMoveSquares(rabbitPosition);
        }

        printWinner(rabbitPosition, turtlePosition);

        tie(rabbitPosition, turtlePosition);
    }

private:
    int randomMove()
    {
        srand(time(NULL));
        int randomInt = rand() % 100 + 1;
        return randomInt;
    }


    int turtleMoveSquares(int tPosition)
    {

        int turtleMove = randomMove();

        if(turtleMove >=1 && turtleMove <= 40)
            tPosition = tPosition + 4;

        if(turtleMove >= 41 && turtleMove <= 50 )
            tPosition = tPosition - 2;

        if(turtleMove >=51 && turtleMove <=100)
            tPosition = tPosition + 2;

        if(tPosition < 1)
            tPosition = 1;

        return tPosition;
    }


    int rabbitMoveSquares(int rabbitPosition)
    {

        int rabbitMove = randomMove();

        if(rabbitMove >=1 && rabbitMove <= 25)
            rabbitPosition = rabbitPosition;

        if(rabbitMove >=26 && rabbitMove <= 55)
            rabbitPosition = rabbitPosition + 10;

        if(rabbitMove >=56 && rabbitMove <=60)
            rabbitPosition = rabbitPosition - 15;

        if(rabbitMove >=61 && rabbitMove <= 90)
            rabbitPosition = rabbitPosition + 5;

        if(rabbitMove >=90 && rabbitMove <=100)
            rabbitPosition = rabbitPosition - 3;

        if(rabbitPosition < 1)
            rabbitPosition = 1;

        return rabbitPosition;
    }

    void printPositions(int rabbitPositions, int turtlePositions)
    {
        int turtleCount;
        int rabbitCount;
        int endCount;

        if(rabbitPositions == turtlePositions && rabbitPositions != 1)
        {
            turtleCount = 1;

            while(turtleCount < turtlePositions)
            {
                cout << "-";
                turtleCount = turtleCount+1;
            }
            cout << "OUCH!";
        }

        else
        {
            turtleCount = 1;
            rabbitCount = 1;
            endCount=1;

            if(turtlePositions < rabbitPositions)
            {
                while(turtleCount < turtlePositions)
                {
                    cout <<  "-";
                    turtleCount = turtleCount+1;
                }

                cout << "T";

                while(rabbitCount < (rabbitPositions - turtlePositions))
                {
                    cout <<  "-";
                    rabbitCount = rabbitCount+1;
                }
                cout << "H";

            }

            if(rabbitPositions < turtlePositions)
            {
                while(rabbitCount < rabbitPositions)
                {
                    cout << "-";
                    rabbitCount = rabbitCount+1;
                }
                cout << "H";

                while(turtleCount < (turtlePositions - rabbitPositions))
                {
                    cout << "-";
                    turtleCount = turtleCount+1;
                }
                cout << "T";

                cout << "\n";
            }
        }
    }

    void printWinner(int rabbitPosition, int turtlePosition)
    {
        if(turtlePosition >= 70 && rabbitPosition < 70)
        {
            cout << "TORTOISE WINS!!! YAY!!!\n";
        }
        else if(rabbitPosition >=70 && turtlePosition < 70)
        {
            cout << "Hare wins. Yuch.\n";
        }
        else if(rabbitPosition >=70 && turtlePosition >=70)
        {
            cout << "It's a tie\n";
        }
    }

    void tie(int turtlePosition, int rabbitPosition)
    {
        if(rabbitPosition >=70 && turtlePosition >=70)
            ExecuteRace();
    }
};


void main()
{
    Race race;
    race.ExecuteRace();
}
SDX2000
Function main always returns int, never void. People who use that as a shibboleth will judge you for it, and your program will have undefined behavior.
Rob Kennedy
"People who use that as a shibboleth" hahaha good one. I am just using the same code as the OP so that he knows what goes where.
SDX2000
+1  A: 

You have no main function.

C++'s biggest difference from Java is that its main() function has this signature (parameters optional):

int main(int argc, char** argv);

And it is in the global namespace. This is the function invoked at program start, and probably why you're missing a symbol.

Try adding this to your program:

int main() {
    Race r;
    r.main();
    return 0;
}

Alternatively, you can do away with the class entirely since you're not actually doing anything with it.

greyfade