C++ C++;主功能更改后功能缺失

C++ C++;主功能更改后功能缺失,c++,function,main,C++,Function,Main,只是为了澄清,目前正在使用Repl.it。如果这个问题是由于Repl.it引起的,那就是了 我试图制造多个状态机,它们通过不同的状态(快乐、悲伤或疯狂)相互影响。每台机器都能说话:说出它们处于什么状态;或者与不同的机器交互,从而改变机器的一种状态 我的代码的问题是everyone函数,它允许数组中的每个状态机都说出它们的状态。每当主函数中发生更改时,everyone函数就不再运行。很抱歉,这篇文章太长了,主要是由于任何遗漏导致功能中断 这是我的代码: using namespace std;

只是为了澄清,目前正在使用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;
}
到目前为止,一切看起来都很好。但是,如果我要添加、更改或删除主函数中的任何行,那么突然之间,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;

问题出在你的功能上

void everyone(StateMac SMar[]) {
  for (int i; i < getSMarSize(SMar); i++) {
    cout << "SM" << i << ": " << SMar[i].talk() << endl;
  }
}
void everyone(政治家【】){
for(inti;iCUT在这个例子中使用你的调试器,数组是用StaseMac()结束的,但是你关于使用矢量或传递大小的评论是有效的(不只是针对更多C++风格的代码,也适用于性能),它的代码会遍历数组n ^ 2次)。。此外,StateMac不会在默认构造函数中初始化其
状态
变量,因此“默认”状态实际上不是默认状态。如何获取数组的大小?对于(int I=0;IStateMac ar[]=…
创建数组时,您知道数组的大小,因此可以使用
unsigned int num=4;StateMac ar[num]或者,…< /代码>。或者,如果切换到使用<代码> STD::vector AR < /代码>,<代码> AR.siz()/Cuth>告诉您长度;查阅关于STL容器的信息的任何好的C++引用。
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!
void everyone(StateMac SMar[]) {
  for (int i; i < getSMarSize(SMar); i++) {
    cout << "SM" << i << ": " << SMar[i].talk() << endl;
  }
}