我之前的第一个C ++程序(这个)遇到了一些问题。基本上我正在尝试为C ++课程做一个任务,教授告诉我们没有语法。这是我现在的代码:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
class Race
{
public:
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();
}
}
};
int main()
{
Race race;
race.main();
return EXIT_SUCCESS;
}
这是我在编译时的错误:
uxb3% g++ o- Race Race.cc
g++: o-: No such file or directory
g++: Race: No such file or directory
Race.cc: In member function 'void Race::main()':
Race.cc:14: error: 'executeRace' was not declared in this scope
Race.cc:17: error: a function-definition is not allowed here before '{' token
Race.cc:24: error: a function-definition is not allowed here before '{' token
Race.cc:44: error: a function-definition is not allowed here before '{' token
Race.cc:64: error: a function-definition is not allowed here before '{' token
Race.cc:90: error: a function-definition is not allowed here before '{' token
Race.cc:153: error: a function-definition is not allowed here before '{' token
Race.cc:169: error: a function-definition is not allowed here before '{' token
很抱歉让你们为这项任务烦恼,但这是我的第一次,而且我现在非常,非常沮丧和痴迷。
答案 0 :(得分:8)
你不能拥有functions inside your functions *。
你可能想要这个:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
class Race
{
public:
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();
}
};
int main()
{
Race race;
race.executeRace();
return EXIT_SUCCESS;
}
*除非处理更高级的语言,例如微积分,当然!
答案 1 :(得分:3)
编译器选项:
g++ -o Race Race.cc
您还在Race :: main函数
中声明了函数class Race
{
public:
void main()
{
executeRace();
} // <----- add this
答案 2 :(得分:2)
我认为你应该使用gcc -o ...
,而不是gcc o- ...
。
其次,你不能用C ++定义带函数的函数。
将其他功能定义移到您的班级void main()
之外:具体而言,您需要将int main()
之前的第二个大括号移到int randomMove()
之前。
答案 3 :(得分:1)
你想要拉出目前在Race(在大括号内)定义的main()函数中包含的各种函数。
选择它们,Ctrl-X(剪切),移动到main()声明上方,Ctrl-V(粘贴)。重新格式化。重新编译。