情况如下:
我刚刚开始了我的第一个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();
}
};
答案 0 :(得分:14)
抱怨main
未定义,这是对的。 C和C ++需要一个名为main
的独立函数,它返回int
。使用名为main
的方法的类是不够的;编译器只关心独立的编译器。
要记住的另一件事是,class
类型成员的默认可见性是私有。要么改变你的班级以使用struct
- 这几乎是两者之间的唯一区别 - 或指定公众可见度:
class Race {
public:
void main() { ... }
};
然后你可以从你的班级调用main
函数:
int main() {
Race race;
race.main();
return EXIT_SUCCESS;
}
答案 1 :(得分:3)
你需要一个main()函数给程序一个起点:
int main()
{
// whatever...
// Maybe:
Race race;
race.main();
}
但是,将许多方法设置为静态方法可能是有意义的,因为它们不依赖于Race
类的任何实例成员。
答案 2 :(得分:3)
您需要一个主要功能:
// 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();
}
左右,没有看到您的具体错误。
答案 3 :(得分:3)
C ++真的不像Java。例如,函数不必是类的一部分。您需要移动函数main(),使其位于类之外。
class Race {
// stuff
};
int main() {
// stuff
}
答案 4 :(得分:2)
也许这就是你的意思
#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();
}
答案 5 :(得分:1)
你没有主要功能。
C ++与Java的最大区别在于它的main()函数具有此签名(参数可选):
int main(int argc, char** argv);
它位于全局命名空间中。这是在程序启动时调用的函数,也可能是您缺少符号的原因。
尝试将此添加到您的程序中:
int main() {
Race r;
r.main();
return 0;
}
或者,您可以完全取消课程,因为您实际上并没有使用它。