简体   繁体   English

错误C2664:在C ++中?

[英]error C2664: in c++?

for (int v = 0; v <= WordChosen.length();v++)
{
    if(Letter == WordChosen[v])
    {
        WordChosenDuplicate.replace(v,1,Letter);
    }
}

I get this error 我得到这个错误

"Error 4 error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::replace(__w64 unsigned int,__w64 unsigned int,const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 3 from 'char' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\\documents and settings\\main\\my documents\\uni\\2nd year\\tp2\\hangman\\hangman\\hangman.cpp 147 " “错误4错误C2664:'std :: basic_string <_Elem,_Traits,_Ax>&std :: basic_string <_Elem,_Traits,_Ax> :: replace(__ w64 unsigned int,__ w64 unsigned int,const std :: basic_string <_Elem,_Traits ,_Ax>&)':无法将参数3从'char'转换为'const std :: basic_string <_Elem,_Traits,_Ax>&'c:\\ documents and settings \\ main \\ mydocuments \\ uni \\ 2nd year \\ tp2 \\ hangman \\ hangman \\ hangman.cpp 147“

I only got the error after putting this line in 将这一行放入后才出现错误

WordChosenDuplicate.replace(v,1,Letter);

The std::string::replace() function's parameters are incorrect or you need to invoke a different overload of replace . std::string::replace()函数的参数不正确,或者您需要调用replace的其他重载。 Something like: 就像是:

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

要么

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

What do you want to achieve? 您想实现什么? The version of replace that you are trying to call doesn't exist – as the compiler is telling you. 您尝试调用的replace版本不存在–编译器告诉您。 Which of these versions do you mean? 您指的是哪个版本

It appears that WordChosenDuplicate is a std::string, in which case the 3rd parameter in the replace() method should be another std::string or a c-style const char*. 看来WordChosenDuplicate是一个std :: string,在这种情况下replace()方法中的第三个参数应该是另一个std :: string或c样式的const char *。 You are trying to pass a single char instead ("Letter"). 您正尝试传递单个字符(“字母”)。 The error is saying that there is no version of replace() that takes a char as the 3rd parameter. 错误的原因是,没有使用char作为第三个参数的replace()版本。

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

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