【问题标题】:error C2664: in c++?错误 C2664:在 C++ 中?
【发布时间】:2010-10-19 20:03:30
【问题描述】:
for (int v = 0; v <= WordChosen.length();v++)
{
    if(Letter == WordChosen[v])
    {
        WordChosenDuplicate.replace(v,1,Letter);
    }
}

我收到这个错误

"错误 4 错误 C2664: 'std::basic_string<_elem> &std::basic_string<_elem>::replace(__w64 无符号整数,__w64 无符号整数,常量 std::basic_string<_elem> &)' : 无法将参数 3 从 'char' 到 'const std::basic_string<_elem> &' c:\documents and settings\main\my 文件\uni\2nd 年\tp2\hangman\hangman\hangman.cpp 147 "

我在把这行放进去后才得到错误

WordChosenDuplicate.replace(v,1,Letter);

【问题讨论】:

  • 请显示更多代码 - 具体显示 WordChosen、WordChosenDuplicate 和 Letter 的定义。
  • 感谢大家的帮助

标签: c++ visual-c++


【解决方案1】:

或者

WordChosenDuplicate.replace(v,1,std::string(Letter, 1));

【讨论】:

    【解决方案2】:

    std::string::replace() 函数的参数不正确,或者您需要调用 replace 的不同重载。比如:

     WordChosenDuplicate.replace(v, // substring begining at index v
                                 1, // of length 1
                                 1, // replace by 1 copy of
                                 Letter); // character Letter
    

    【讨论】:

      【解决方案3】:

      你想达到什么目标?您尝试调用的replace 版本不存在——正如编译器告诉您的那样。你指的是these versions中的哪一个?

      【讨论】:

      • 对不起,我一直在关注这本书,他们说“str1.replace(m,n,str2) 和参数 m 和 n 的类型为 string::size_type”
      • 你的第三个参数必须是 string 类型,但是(不是 char)。
      【解决方案4】:

      WordChosenDuplicate 似乎是一个 std::string,在这种情况下,replace() 方法中的第三个参数应该是另一个 std::string 或 c 样式的 const char*。您正在尝试传递单个字符(“字母”)。错误是说没有将 char 作为第三个参数的 replace() 版本。

      【讨论】:

        猜你喜欢
        • 2014-06-29
        • 1970-01-01
        • 2014-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多