刚开始C ++:编译时未定义的符号错误 [英] Just started C++: Undefined Symbol error on Compile

查看:160
本文介绍了刚开始C ++:编译时未定义的符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是情况:

我刚刚开始我的第一个C ++类。我对C ++一无所知,尽管这是C ++类的介绍,老师已经为语言教了很多NO语法,只是我们已经知道的逻辑。

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.

现在他给了我们一个程序描述,并说,写,哈哈。我理解的逻辑罚款,但正如我所说,我不知道C ++。因此,我首先在java(我知道)写它,然后一旦我在java中工作,我试图将其编码到C ++。

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

我编译时遇到以下错误

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

有人可以帮助我

这是我的代码在.txt文件中:
http://rapidshare.com/files/195742284/race.txt.html

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();
        }
};


推荐答案

/ code>未定义,这是正确的。 C和C ++需要一个名为 main 独立函数,它返回 int 。使用名为 main 的方法拥有是不够的;编译器只关心独立的一个。

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.

另一件要记住的是,类中的成员的默认可见性类型是私人。或者更改你的类使用 struct - 这是两者之间的唯一区别 - 或指定公开的可见性:

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() { ... }
};

然后你可以调用 main 你的类:

Then you can call the main function from your class:

int main() {
  Race race;
  race.main();
  return EXIT_SUCCESS;
}

这篇关于刚开始C ++:编译时未定义的符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆