情况如下:
我刚开始我的第一个 C++ 课程。我对 C++ 一无所知,尽管这是对 C++ 课程的介绍,但老师几乎没有教过该语言的语法,只是我们已经知道的逻辑。
现在他给了我们一个程序描述并说,“写吧,哈哈。” 我理解逻辑很好,但正如我之前所说,我对 C++ 一无所知。因此,我首先用 java 编写了它(我确实知道),然后一旦我让它在 java 中工作,我就尝试将它编码为 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
这是复制粘贴:
#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();
}
};