简体   繁体   English

main更改后缺少C ++函数

[英]C++ function missing after change in main

Just for clarification, currently using Repl.it. 为了澄清起见,当前使用Repl.it。 If this problem is due to Repl.it, that'll be it. 如果此问题是由于Repl.it造成的,那么就是这样。

I am trying to make multiple state machines that influence each other through different states (Happy, Sad, or Mad). 我试图制作多个状态机,这些状态机通过不同的状态(快乐,悲伤或疯狂)相互影响。 Each machine can talk: say what state they're in; 每台机器都可以讲话:说出他们所处的状态; or interact with a different machine, thus changing one of the machine's states; 或与另一台机器交互,从而改变机器的状态之一;

The problem I have with my code is the everyone function, allowing every state machine in an array to say their states. 我的代码存在的问题是每个人的功能,使数组中的每个状态机都能说出自己的状态。 Whenever something is changed in the main function, the everyone function just doesn't run anymore. 只要主函数中的任何内容发生更改,everything函数就不再运行。 I'm sorry that this post is really long, mostly due to any omissions causes the function to break. 很抱歉,这篇文章真的很长,主要是由于任何遗漏导致功能中断。

This is my code: 这是我的代码:

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;
}

Producing this result: 产生此结果:

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!

Everything looks good in the result as of now. 到目前为止,一切看起来都不错。 However, if I am to add, change, or delete any line in the main function, all of a sudden, the everyone function doesn't run anymore. 但是,如果我要添加,更改或删除主函数中的任何行,突然之间,所有人函数将不再运行。

For example, I changed one of the responses in the main function: 例如,我更改了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;

Creating this result, notice the missing everyone function call: 创建此结果时,请注意缺少每个人的函数调用:

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!

The problem is in your function 问题出在你的职能上

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

This code does not initialize the variable i , and so it has undefined behavior. 此代码不会初始化变量i ,因此它具有未定义的行为。 Changing this to int i = 0 should solve your problem. 将其更改为int i = 0应该可以解决您的问题。

BTW, I am also puzzled by your use of the function int getSMarSize(StateMac SMar[]) to determine the size of the array of state machines. 顺便说一句,您使用int getSMarSize(StateMac SMar[])函数确定状态机数组的大小也使我感到困惑。 Your strategy seems to be to leave a "blank" state machine at the end of the array and counts the array length by iterating until it finds this blank state machine, much like the terminating character in a C string. 您的策略似乎是将“空白”状态机留在数组的末尾,并通过迭代直到找到该空白状态机来对数组长度进行计数,这与C字符串中的终止字符非常相似。 Since you cannot automatically enforce that the array should end in StateMac() , unlike C/C++ can do with a C string, this is error-prone. 由于您不能自动强制数组应以StateMac()结尾,因此与C / C ++可以使用C字符串不同,这很容易出错。 There is no good reason to use this technique for an array in C++ -- you should either pass the length of the array as a parameter to the function, or better yet use the std::vector container. 没有充分的理由在C ++中对数组使用此技术-您应该将数组的长度作为参数传递给函数,或者更好地使用std::vector容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM