这是我第一次使用C ++处理类,而且我似乎被很多绊倒了。我的程序应该是对使用struct的先前程序的重写(请参见此处:for循环中的随机数生成器每次都会给出相同的数字),但改用了一个类。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int WHEEL_POSITIONS = 30;
const char wheelSymbols[WHEEL_POSITIONS + 1] = "-X-X-X-X-X=X=X=X*X*X*X*X@X@X7X";
class slotMachine
{
private:
int spinPos;
char spinSymbol;
public:
slotMachine(); // Constructor
char symbols[WHEEL_POSITIONS + 1]; // Should be private?
void setSpinSymbol(); // Spins the wheels
char getSpinSymbol() const // Returns the symbol
{ return spinSymbol; }
} wheels[3];
// Constructor initializes slot wheels to contents of wheelSymbols
slotMachine::slotMachine()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < (WHEEL_POSITIONS + 1); j++)
{
wheels[i].symbols[j] = wheelSymbols[j];
}
}
}
void slotMachine::setSpinSymbol()
{
for (int i = 0; i < 3; i++)
{
wheels[i].spinPos = (rand() % WHEEL_POSITIONS);
wheels[i].spinSymbol = wheels[i].symbols[(wheels[i].spinPos)];
}
}
void displayResults(slotMachine fwheels[3])
{
for (int i = 0; i < 3; i++)
{
cout << fwheels[i].getSpinSymbol();
}
}
void displayResults(slotMachine []);
//bool getWinner(slotMachine []);
int main(void)
{
slotMachine wheels[3];
time_t seed;
time(&seed);
srand(seed);
displayResults(wheels);
return 0;
}
该代码可以编译,但是输出以下内容:
我有一种感觉这个错误是通过具有在我的构造走了不对劲的东西造成的slotMachine
,我的getSpinSymbol()
功能,或者我的setSpinSymbol()
功能,但我已经看了过来几次,似乎无法弄清楚。我已经阅读了几篇有关C ++类的在线材料,但是对于这个概念,我还是很新奇,也很不稳定-如果我忽略了很小或很明显的东西,我深表歉意。
您的代码有几个问题:
1.Class names should be started with upper case letter. slotMachine -> SlotMachine
2.Remove wheels[3] after class definition.You are using the array declared in main() method.
3.Why you are declaring displayResults(..) again after it's definition?
4.You are not calling setSpinSymbol() before displayResults(..).
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句