【问题标题】:error: expected ‘;’ before ‘generationString’ [closed]错误:预期的';'在'generationString'之前[关闭]
【发布时间】:2018-10-24 12:56:28
【问题描述】:

我目前正在尝试解决我在使用此代码时遇到的一些问题,但无法真正弄清楚为什么会出现这 2 个错误。 我试图查看是否有东西没有关闭,但这似乎不是这种情况,可能是“:”之间的距离造成的? 我现在只是在抓稻草..

main.cpp:30:38: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x << ": " generationString << endl;

main.cpp:54:40: error: expected ‘;’ before ‘generationString’
cout << "Generation " << x++ << ": " generationString << endl;

尝试编译此代码时:

#include <iostream>

using namespace std;

string
initString ()
{
}

int
calculateScore (string guess, string target)
{
}

string
mutate (string mutationString)
{
}


int
main ()
{
  string targetString = "METHINKS IT IS LIKE A WEASEL";
  string generationString = initString ();
  string currentString = generationString;

  int score = calculateScore (currentString, targetString);
  int x = 0;
  cout << "Generation " << x << ": " generationString << endl;
  do
    {
      for (int i = 0; i < 100; i++)
    {
      string newCopy = generationString;
      newCopy = mutate (newCopy);
      int copyScore = calculateScore (newCopy, targetString);

      if (copyScore > score)
        {
      currentString = newCopy;
      score = copyScore;
      if (copyScore == targetString.length ())
    {
      break;
    }

    }
}
  generationString = currentString;

     }
  while (score < targetString.length ());
  cout << "Generation " << x++ << ": " generationString << endl;
  return 0;
  }

【问题讨论】:

  • ": " generationString -> ": " &lt;&lt; generationString。投票结束是错字。
  • 是的,肯定是错字问题。如果您单独发布此内容,我可以添加为答案。
  • 错字问题应该被标记为这样。不予回答。

标签: c++ output ostream


【解决方案1】:

您缺少&lt;&lt;

cout << "Generation " << x << ": " generationString << endl;

应该是

cout << "Generation " << x << ": " << generationString << endl;

同样

cout << "Generation " << x++ << ": " generationString << endl;

应该是这样的

cout << "Generation " << x++ << ": " << generationString << endl;

【讨论】:

    【解决方案2】:

    您可能在该行中缺少&lt;&lt;

    cout << "Generation " << x << ": " generationString << endl;
    

    给你

    ": " generationString
    

    应该是

    ": " << generationString
    

    C++ 可以连接文字字符串,但它不能连接文字字符串和其他任何东西(比如 std::strings)。所以例如这会起作用

    cout << "Generation " << x << ": " "METHINKS IT IS LIKE A WEASEL" << endl;
    

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2023-03-31
      • 1970-01-01
      • 2013-09-23
      • 2014-11-10
      • 2018-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多