【发布时间】:2017-08-26 18:34:53
【问题描述】:
只是为了澄清,目前使用 Repl.it。如果这个问题是由 Repl.it 引起的,那就是它。
我正在尝试制作多个通过不同状态(快乐、悲伤或疯狂)相互影响的状态机。每台机器都可以说话:说出它们所处的状态;或与不同的机器交互,从而改变其中一个机器的状态;
我的代码存在的问题是每个人的功能,它允许数组中的每个状态机说出它们的状态。每当 main 函数中的某些内容发生更改时,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!
到目前为止,结果中的一切看起来都不错。但是,如果我要在 main 函数中添加、更改或删除任何一行,突然之间,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;
创建此结果,请注意缺少的所有人函数调用:
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!
【问题讨论】:
-
使用你的调试器