【问题标题】:std::string manipulating error on C++Builder 6C ++ Builder 6上的std :: string操作错误
【发布时间】:2015-01-19 10:20:25
【问题描述】:

我正在使用 Emericc 斧头为项目创建一个类。此类的目标是使用错误帧返回消息。我必须只使用 STL 的 std::string 变量。 但是,IDE 无法识别任何一个查找函数之后的字符串操作。

请注意,我是法国人,所以我使用的变量是我的语言(代码非常简单)。

我遇到的错误在这个函数中:

string ErrMericc::ErrTrame(string trame)
{
  /*
   trame(fr) = frame(eng)
   virgule(fr) = comma(eng) [I shortened "virgule" to "virg" in a variable]
  */

  this->trame=trame;
  trame.find('!', posExcl); //this marks the beginnig of the error number

  if(posExcl == string::npos)
  {
    trame.clear(); //clearing in case the variable is not empty
    //in case this is not an error frame
    trame.push_back("Erreur 200 : "+errors[200]+". La trame envoyée n'est pas une trame d'erreur."); 
    return trame; 
  }
  else
  {
    trame.find(',', posVirg); //here is the error
    //starting here, no function using a string is recognized by the IDE
    //and I don't find why
    nb.push_back(trame.substr(posExcl+2, posVirg)); //unrecognized push_back() and substr()

    errNb=StrToInt(trame.c_str());
    trame=errors[errNb];
    return "Erreur "+IntToStr(errNb)+" : "+trame;
  }

}

C++Builder 6 告诉我:[C++ 错误] ErrMericc.cpp(1): 由于源代码错误,无法调用代码执行。 当我双击此错误消息时,它会将我带到文件中源代码的第一行。

除非我注释掉“trame.find(',', posVirg);”,否则错误消息似乎不会停止弹出。

你能解释一下我的错误在哪里吗?

编辑:

看起来 C++ Builder 6 只是不喜欢代码行...

这行得通:

    trame.find(',', posVirg); //here is the error
    //starting here, no function using a string is recognized by the IDE
    //and I don't find why
    nb.push_back(trame.substr(posExcl+2, posVirg)); //unrecognized push_back() and substr()

这不是:

    //here is the error
    //starting here, no function using a string is recognized by the IDE
    //and I don't find why
    trame.find(',', posVirg); 
    nb.push_back(trame.substr(posExcl+2, posVirg)); //unrecognized push_back() and substr()

很奇怪。

我开始认为问题出在 IDE 上。 到目前为止,我已经尝试将文件复制并移动到新文件夹中,但故障不受影响。

【问题讨论】:

  • 你有更准确的错误信息吗?恐怕目前的信息太模糊,无法诊断问题。另请注意,C++ Builder 6 于 2002 年发布,这使其成为一个非常古老且过时的编译器。
  • @SirDarius 不幸的是我没有...这是screencap,也许它可以提供帮助。这是一个学校项目,所以我必须使用那个版本(虽然我会喜欢更新的东西)。
  • 这与您在问题中引用的错误相同。什么是“源代码错误”?这是相当令人惊讶的。据我记得,当我使用 C++ Builder 时,它的错误消息总是对我很有帮助。
  • 我在一些 IRL 帮助和有关 std::strings 的文档中发现了错误。 @SirDarius 感谢您的时间和帮助 :)
  • 在旁注中,您误用了std::string::find()。第二个参数是 input 参数(搜索的起始位置),但您将其视为 output 参数(搜索的结果)。将trame.find('!', posExcl) 更改为posExcl = trame.find('!'),将trame.find(',', posVirg) 更改为posVirg = trame.find(',', posExcl+2),并将nb.push_back(trame.substr(posExcl+2, posVirg)) 更改为nb = trame.substr(posExcl+2, posVirg-(posExcl+2))。此外,StrToInt(trame.c_str()) 总是会由于标点符号而失败,您可能想要 StrToInt(nb.c_str()) 代替。

标签: c++ string frame c++builder c++builder-6


【解决方案1】:

原来错误来自我使用 push_back() 函数:我一次只能推回一个字符。
在一个内存位置输入多个值,使其包含一个字符,这不是一个好主意。

显然,C++Builder 6 只是不知道该怎么做,因为错误来自我在 stl 中的使用。

所以我不应该这样做:

trame.push_back("Erreur 200 : "+errors[200]+". La trame envoyée n'est pas une trame d'erreur.");

但是:

trame = string("Erreur 200 : ") + errors[200] + string(". La trame envoyée n'est pas une trame d'erreur.");

【讨论】:

  • std::stringchar 值的容器,因此push_back() 只接受char 值。就像 std::vector<T> 可以 push_back() 只有 T 值。在每个容器类型中,push_back() 只接受容器元素类型的值。
  • 在这种情况下,如果我真的想使用一个函数,我应该使用std::string::append()。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多