以下代碼在某些環境中為我正確編譯(例如,在帶有 GCC 9.3.0 的編譯器資源管理器上)并在其他環境中抱怨(CentOS 7.9.2009 (Core)、GCC 9.3.1)。
#include <iostream>
#include <string>
using namespace std;
int main() {
std::string name="aakash";
name.erase(name.begin() 2, name.cend());
return 0;
}
當我收到錯誤時,錯誤是:
test.cpp:6:40: error: no matching function for call to 'std::basic_string<char>::erase(__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >, std::basic_string<char>::const_iterator)'
6 | name.erase(name.begin() 2, name.cend());
| ^
In file included from /opt/rh/devtoolset-9/root/usr/include/c /9/string:55,
from /opt/rh/devtoolset-9/root/usr/include/c /9/bits/locale_classes.h:40,
from /opt/rh/devtoolset-9/root/usr/include/c /9/bits/ios_base.h:41,
from /opt/rh/devtoolset-9/root/usr/include/c /9/ios:42,
from /opt/rh/devtoolset-9/root/usr/include/c /9/ostream:38,
from /opt/rh/devtoolset-9/root/usr/include/c /9/iostream:39,
from test.cpp:1:
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4698:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::size_type, std::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]'
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4698:23: note: no known conversion for argument 1 from '__gnu_cxx::__normal_iterator<char*, std::basic_string<char> >' to 'std::basic_string<char>::size_type' {aka 'long unsigned int'}
4698 | erase(size_type __pos = 0, size_type __n = npos)
| ~~~~~~~~~~^~~~~~~~~
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4714:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4714 | erase(iterator __position)
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4714:7: note: candidate expects 1 argument, 2 provided
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4734:7: note: candidate: 'std::basic_string<_CharT, _Traits, _Alloc>::iterator std::basic_string<_CharT, _Traits, _Alloc>::erase(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]'
4734 | erase(iterator __first, iterator __last);
| ^~~~~
/opt/rh/devtoolset-9/root/usr/include/c /9/bits/basic_string.h:4734:40: note: no known conversion for argument 2 from '__normal_iterator<const char*,[...]>' to '__normal_iterator<char*,[...]>'
4734 | erase(iterator __first, iterator __last);
| ~~~~~~~~~^~~~~~
| ~~~~~~~~~^~~~~~
這個錯誤對我來說看起來很合理,因為根據C 檔案,兩個引數都std::basic_string::erase應該是 typeiterator或const_iterator.
那么,當它作業時(例如這里),是什么讓它作業?
uj5u.com熱心網友回復:
從 C 11 開始,雙迭代器多載的兩個std::string::erase()引數都是const_iterator. 請參閱此 cppreference 頁面上的表格#3 (您在問題中鏈接的那個)。
此外,C 11 標準要求容器的iterator型別可以轉換為等價的const_iterator.
從這個 C 11 標準草案中,在§23.2.1 [container.requirements.general]中,表 96具有以下條目(粗體強調我的):
| 表達 | 回傳型別 | 操作語意 | 斷言/注釋 | … |
|---|---|---|---|---|
X::iterator |
值 型別為的迭代器型別 T |
任何 滿足前向迭代器 要求的迭代器類別。可轉換為 X::const_iterator. |
… |
因此,在您對 的呼叫中.erase(),第一個引數被轉換為string::const_iterator.
因此,假設您已將它們設定為使用 C 11(或更高版本)標準,則錯誤出現在那些不接受您的呼叫的編譯器中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/513370.html
標籤:C c 11
