2009-04-16 110 views
1
for (int v = 0; v <= WordChosen.length();v++) 
{ 
    if(Letter == WordChosen[v]) 
    { 
     WordChosenDuplicate.replace(v,1,Letter); 
    } 
} 

我得到這個錯誤錯誤C2664:在C++中?

「錯誤4錯誤C2664: 「的std :: basic_string的< _Elem,_Traits,_AX> &的std :: basic_string的< _Elem,_Traits,_AX> ::更換(__w64 無符號整型,__ W64無符號整型,常量 的std :: basic_string的< _Elem,_Traits,_AX> &) ':不能從 轉換參數3 '字符' 到' 常量 的std :: basic_string的< _Elem ,_Traits,_AX> &「C:\ Documents和Settings \ MAIN \我的文檔 \單\第二 年\ TP2 \劊子手\劊子手\ hangman.cpp 147 」

我只得到了錯誤將此行放入後

WordChosenDuplicate.replace(v,1,Letter); 
+1

請出示更多的代碼 - 特別展示WordChosen,WordChosenDuplicate和字母的定義。 – 2009-04-16 11:38:35

+0

謝謝你們的幫助 – user91566 2009-04-16 14:06:39

回答

3

或者

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

你想實現什麼?您試圖調用的replace版本不存在 - 正如編譯器告訴您的那樣。 these versions你的意思是?

3

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 
1

看來,WordChosenDuplicate是爲std :: string,在這種情況下,在replace()方法的第三個參數應該是另一種的std :: string或C風格爲const char *。你正試圖傳遞一個單一的字符(「Letter」)。該錯誤是說沒有版本的replace()將char作爲第三個參數。