2017-07-25 30 views
3

我的問題與this one類似,但沒有示例代碼,我沒有發現它有幫助。:: insert

我的錯誤是

調用類 '的std :: __ 1 :: __ wrap_iter'

的私有構造我的問題的一個小例子,可以發現如下:

#include <iostream> 
#include <string> 

using namespace std; 

int main(){ 
    string g = "this_word"; 
    cout << g << endl; 
    char temp = g[0]; 
    g.erase(0,1); 
    cout << g << endl; 
    g.insert(0,temp); // Compiler seems to dislike this. Why? 
    cout << g << endl; 
    return 0; 
} 

我試過這兩個編譯器,和相同的錯誤。儘可能從標準文檔中閱讀,但不明白我的錯誤。

回答

5

最好檢查所有std::string::insert()過載的簽名,然後決定使用哪一個。 g.insert(0,temp);只是不匹配其中的任何一個。

用於插入char,你可以傳遞一個迭代器像

g.insert(g.begin(), temp); 

或通過指標和算在一起:

g.insert(0, 1, temp); 
+3

還有一個重載需要一個指標,計數和'char'所以'g.insert(0,1,temp);'會工作。 – Blastfurnace

+0

@Blastfurnace好主意,加入到答案中。 – songyuanyao

+0

非常有幫助。我會注意到,我最終傳遞了一個字符串,這是一個竅門。 g.insert(0,charToString(savedLetter)); –