我正在研究一个模具类,它允许用户输入模具上的边数并输出每个模具的轧辊数量和结果数量。我是新手,我知道我搞砸了,但现在无法解决这些问题,因为链接器在Xcode和代码块中给我一个错误。
确切的错误是:
Undefined symbols for architecture x86_64:
"GameDie::GameDie()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的程序代码:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int x = 0;
int sides;
class GameDie
{
public:
GameDie();
int roll();
void getNumSides(int numSides);
void getNumRolls();
private:
int numRolls;
int numSides;
};
int main()
{
srand(time(0));
cout << "Enter number of sides: ";
cin >> sides;
GameDie die1;
die1.roll();
die1.getNumSides(sides);
die1.getNumRolls();
return 0;
}
int GameDie::roll()
{
return (rand() % sides) + 1;
}
void GameDie::getNumSides(int sides)
{
numSides = sides;
}
void GameDie::getNumRolls()
{
x++;
cout << "Roll " << x << " of die with " << sides << " sides";
}
答案 0 :(得分:0)
不确定这个模具应该如何操作但是我接受了你的代码并尽可能少地改变它以使其正常运行。它主要是对方法/成员函数的误解。我希望这会有所帮助。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int x = 0;
int sides;
class GameDie
{
public:
GameDie();
int roll();
void getNumSides(int numSides);
void getNumRolls();
private:
int numRolls;
int numSides;
};
int main()
{
srand(time(0));
cout << "Enter number of sides: ";
cin >> sides;
GameDie die1;
die1.roll();
die1.getNumSides(sides);
die1.getNumRolls();
return 0;
}
GameDie::GameDie()
{
numSides = 0;
}
int GameDie::roll()
{
return (rand() % sides) + 1;
}
void GameDie::getNumSides(int sides)
{
numSides = sides;
}
void GameDie::getNumRolls()
{
x++;
cout << "Roll " << x << " of die with " << sides << " sides";
}