2015-05-22 52 views

回答

2

看着http://en.cppreference.com/w/cpp/string/basic_string/erase(1)你看,你需要傳遞的第一個字符的指數擦除和多少個字符刪除此號碼。你傳遞一個迭代器作爲第一個參數。只要做到

word3 = word2.erase(0, word.length()/2 -1); 
//      ^^^^ 
//      this should probably be word2 

或使用(3)重載接受範圍:

word3 = word2.erase(word2.begin(), std::next(word2.begin(), word2.length()/2 -1)); 

我也相信你應該有你的word2erase,不word內。

+0

謝謝.....已解決 –

0

類的成員函數begin()返回迭代器。這是其返回類型std::string::iteratorstd::string::const_iterator

您正在嘗試使用成員函數erasestd::string::size_type類型的參數:

basic_string& erase(size_type pos = 0, size_type n = npos); 

如果你想使用這個成員函數,你應該寫這樣

word2.erase(0, word.length()/2 - 1) 

如果你想呼叫使用使用以下成員函數的迭代器的函數

iterator erase(const_iterator first, const_iterator last); 

則呼叫可以像

word2.erase(word.begin(), std::next(word.begin(), word.length()/2 - 1)) 

或者乾脆

word2.erase(word.begin(), word.begin() + word.length()/2 - 1) 

我希望這不會是你調用函數對象與名稱word2,並在用作參數表達的錯字你使用對象名稱word

相關問題