主要更改后C ++函数丢失

时间:2017-04-01 15:07:33

标签: c++ function main

仅供澄清,目前正在使用Repl.it.如果这个问题是由Repl.it造成的,那就是它。

我正在尝试制作多个状态机,它们通过不同的状态(Happy,Sad或Mad)相互影响。每台机器都可以说话:说出他们所处的状态;或者与不同的机器交互,从而改变机器的一个状态;

我的代码存在的问题是每个人的功能,允许阵列中的每个状态机说出他们的状态。每当主函数中的某些内容发生变化时,每个函数都不再运行。对不起,这个帖子真的很长,主要是由于任何遗漏导致功能中断。

这是我的代码:

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函数中添加,更改或删除任何行,突然之间,每个人的函数都不再运行。

例如,我更改了main函数中的一个响应:

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!

1 个答案:

答案 0 :(得分:0)

问题在于您的功能

void everyone(StateMac SMar[]) {
  for (int i; i < getSMarSize(SMar); i++) {
    cout << "SM" << i << ": " << SMar[i].talk() << endl;
  }
}

此代码不初始化变量i,因此它具有未定义的行为。将此更改为int i = 0可以解决您的问题。

BTW,我也对使用函数int getSMarSize(StateMac SMar[])来确定状态机数组的大小感到困惑。你的策略似乎是留下一个空白&#34;数组末尾的状态机,通过迭代计算数组长度,直到找到这个空状态机,就像C字符串中的终止字符一样。由于您无法自动强制执行数组应以StateMac()结尾,因此与C / C ++不同,C字符串可以使用C字符串,这很容易出错。没有充分的理由在C ++中将此技术用于数组 - 您应该将数组的长度作为参数传递给函数,或者更好地使用std::vector容器。