【问题标题】:C++ function missing after change in main更改 main 后缺少 C++ 函数
【发布时间】: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!

【问题讨论】:

  • 使用你的调试器

标签: c++ function main


【解决方案1】:

问题出在你的函数中

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 容器。

【讨论】:

  • 在这个例子中,数组 is 由 StateMac() 结束,但是关于使用向量或传递大小的 cmets 是有效的(不仅适用于更多 c++ 样式代码,而且适用于性能 -代码将遍历该数组 N^2 次)。此外,StateMac 不会在默认构造函数中初始化其 state 变量,因此“默认”状态实际上不是默认值。
  • 我应该如何获得数组的大小?,对于 (int i=0; i
  • @Vicky 如果您选择使用数组,您在使用StateMac ar[] = ... 创建数组时就知道数组的大小,因此您可以使用unsigned int num = 4; StateMac ar[num] = ...。或者,如果您切换到使用std::vector&lt;StateMac&gt; arar.size() 会告诉您长度;有关 STL 容器的信息,请参阅任何好的 C++ 参考。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2021-10-07
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多