为了澄清起见,目前正在使用Repl.it。如果这个问题是由于Repl.it造成的,那就是它了。
我试图使多个状态机通过不同的状态(快乐、悲伤或疯狂)相互影响。每台机器都可以说话:说出它们所处的状态;或与不同的机器交互,从而改变机器的一种状态;
我的代码存在的问题是everyone函数,允许数组中的每个状态机表示它们的状态。每当主函数中有什么变化时,everyone函数就不再运行了。很抱歉,这篇文章太长了,主要是因为任何遗漏导致功能中断。
这是我的密码:
using namespace std;
enum Mood {Happy, Sad, Mad, Default};
class StateMac {
Mood state; //The machine's current state
/* Other methods no shown */
//Returns a string relative to their current state
string talk() {
switch(state) {
case Happy : return "I'm happy!";
case Sad : return "I'm sad...";
case Mad : return "I'm Mad!!!";
case Default : return "...";
}
}
//Compares the states between two machines
bool compare(StateMac aStateMachine) {
if (state == aStateMachine.getState()) {
return true;
}
return false;
}
};
//Gets size of a state machine array by comparing each to a default machine
int getSMarSize(StateMac SMar[]) {
int counter = 0;
for (int i = 0; i < 100; i++) {
if (SMar[i].compare(StateMac())) {
break;
} else {
counter += 1;
}
}
return counter;
}
//Receives an array of state machines and makes each of them say their states,
void everyone(StateMac SMar[]) {
for (int i; i < getSMarSize(SMar); i++) {
cout << "SM" << i << ": " << SMar[i].talk() << endl;
}
}
int main() {
//Array with 4 state machines
StateMac ar[] = {StateMac(Happy), StateMac(Sad), StateMac(Mad), StateMac()};
//Have everyone say their states
everyone(ar);
//Does same as above but line-by-line for each machine
cout << "SM0: " << ar[0].talk() << endl;
cout << "SM1: " << ar[1].talk() << endl;
cout << "SM2: " << ar[2].talk() << endl;
//Other functions
string response = ar[0].interact(&ar[2]);
cout << "SM0 to SM1: " << response << endl;
cout << "SM1: " << ar[1].talk() << endl;
response = ar[0].interact(&ar[2]);
cout << "SM0 to SM2: " << response << endl;
cout << "SM2: " << ar[2].talk() << endl;
response = ar[1].interact(&ar[2]);
cout << "SM0 to SM2: " << response << endl;
cout << "SM0: " << ar[0].talk() << endl;
cout << "SM1: " << ar[1].talk() << endl;
cout << "SM2: " << ar[2].talk() << endl;
}产生这一结果:
SM0: I'm happy! //From everyone function
SM1: I'm sad...
SM2: I'm Mad!!!
SM0: I'm happy! //From line-by-line
SM1: I'm sad...
SM2: I'm Mad!!!
SM0 to SM1: There's nothing to be mad about! //Other functions
SM1: I'm sad...
SM0 to SM2: That guy!!!
SM2: I'm happy!
SM0 to SM2: You look happy, might as well forget about that.
SM0: I'm Mad!!!
SM1: I'm sad...
SM2: I'm happy!从现在起,一切看起来都很好。但是,如果我要添加、更改或删除主函数中的任何行,那么突然之间,everyone函数就不再运行了。
例如,我更改了主函数中的一个响应:
everyone(ar);
cout << "SM0: " << ar[0].talk() << endl;
cout << "SM1: " << ar[1].talk() << endl;
cout << "SM2: " << ar[2].talk() << endl;
string response = ""; //Changed here
cout << "SM0 to SM1: " << response << endl;
cout << "SM1: " << ar[1].talk() << endl;
response = ar[0].interact(&ar[2]);
cout << "SM0 to SM2: " << response << endl;
cout << "SM2: " << ar[2].talk() << endl;
response = ar[1].interact(&ar[2]);
cout << "SM0 to SM2: " << response << endl;
cout << "SM0: " << ar[0].talk() << endl;
cout << "SM1: " << ar[1].talk() << endl;
cout << "SM2: " << ar[2].talk() << endl;创建此结果时,请注意缺少的everyone函数调用:
SM0: I'm happy! //Line-by-line
SM1: I'm sad...
SM2: I'm Mad!!!
SM0 to SM1: //Changed response
SM1: I'm sad...
SM0 to SM2: There's nothing to be mad about!
SM2: I'm happy!
SM0 to SM2: You look happy, might as well forget about that.
SM0: I'm happy!
SM1: I'm sad...
SM2: I'm happy!发布于 2017-04-01 15:30:42
问题在于你的功能
void everyone(StateMac SMar[]) {
for (int i; i < getSMarSize(SMar); i++) {
cout << "SM" << i << ": " << SMar[i].talk() << endl;
}
}此代码不初始化变量i,因此它具有未定义的行为。将其更改为int i = 0可以解决您的问题。
顺便说一句,我也对您使用函数int getSMarSize(StateMac SMar[])来确定状态机数组的大小感到困惑。您的策略似乎是在数组的末尾保留一个“空白”状态机,并通过迭代来计算数组长度,直到它找到这个空白状态机,就像C字符串中的终止字符一样。由于不能自动强制数组以StateMac()结尾,因此与C/C++可以使用C字符串不同,这很容易出错。对于C++中的数组,没有很好的理由使用这种技术--您应该将数组的长度作为参数传递给函数,或者更好地使用std::vector容器。
https://stackoverflow.com/questions/43158448
复制相似问题