c++ stl映射编译问题与GCC/代码块
C++ stl map compiler problem with GCC/codeblocks
下面的代码应该与VS 2008一起工作,但我在第53行周围遇到了麻烦地点:
portMap_.erase(it);
我正在使用codeblocks/mingw/gcc 4.xx
#include<iostream>
#include<map>
#include<string>
using namespace std;
//defining a union that is used with newMap_
union uu
{
char c;
int i;
} u;
//Lets define two different maps
//The first parameter is key and second value
map<string, int> portMap_;
map<void *, uu> newMap_;
int main()
{
//first entry in portmap
portMap_["first"] = 1;
//example of using the iterator
map<string, int>::const_iterator it;
string z = "second";
it = portMap_.find(z); //not in the map so wont be found
if(it == portMap_.end())
{
portMap_[z] = 22; //add second element
}
//Add thrid element directly
z = "third";
portMap_[z] = 12345;
//Add 4th element by insert
portMap_.insert(pair<string,int>("fourth", 4444));
//Add 5th element by insert
portMap_.insert(pair<string,int>("fifth", 5555));
cout<<"n** Printing the portmap_ values **"<<endl;
for(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;
cout<<"n** Removing fourth element **"<<endl;
z = "fourth";
it = portMap_.find(z);
portMap_.erase(it);
cout<<"n** Printing the portmap_ values **"<<endl;
for(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;
//Playing with New Map
cout<<"nnCreating New Map whose key is a void pointer"<<endl;
uu u_val1, u_val2;
void *val1, *val2;
u_val1.i = 70, val1 = &u_val1;
newMap_[val1]=u_val1;
val2 = val1;
map<void *, uu>::const_iterator it_new;
it_new = newMap_.find(val2);
if(it_new != newMap_.end())
{
u_val2 = it_new->second;
cout<<"Note that since u_val2 is a union you can print i or c as required"<<endl;
cout<<"val2 = "<<val2<<" value.c = "<<u_val2.c<<endl;
cout<<"val2 = "<<val2<<" value.i = "<<u_val2.i<<endl;
}
return 0;
}
错误如下:
map_examplemap_example.cpp||In function 'int main()':|
map_examplemap_example.cpp|51|error: no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> > >::erase(std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int> >&)'|
c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++bitsstl_map.h|566|note: candidates are: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char,|
c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++bitsstl_map.h|581|note: typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::size_type std::map<_Key, _Tp, _Compare, _Alloc>::erase(const _Key&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = int, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_stri|
c:program filescodeblocksmingwbin..libgccmingw324.4.1includec++bitsstl_map.h|596|note: void std::map<_Key, _Tp, _Compare, _Alloc>::erase(typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator, typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::other>::iterator) [with _Key = std::basic_string<char, std::char_traits<char>, std::all|
||=== Build finished: 1 errors, 0 warnings ===|
你的迭代器定义为const。试试非常量迭代器
还可以通过简单地使用key_type来擦除元素。在您的示例中,它将是:
portMap_.erase(z);
尝试从map<string, int>::const_iterator it;
到map<string, int>::iterator it
。
相关文章:
- GCC对可能有效的代码抛出init list生存期警告
- GCC 和 Clang 代码性能的巨大差异
- GCC 能否优化具有相同主体的函数的代码大小?
- GCC 编译器是否应该对涉及 [[fallthrough]] 属性的格式错误的C++代码进行诊断?
- 如何使用 gcc 通过命令行限制C++代码的内存使用量?
- MSVC对这段代码很满意,但GCC并不那么热衷
- 为什么这段代码无法使用 gcc 4.8.5 编译,而使用 clang 编译正常
- 为什么以下代码在在线 ide(gcc 7.2.0) 上有效,但在 ubuntu 上出现错误?
- Boost tread 代码在 VC++ 下编译,但不在 GCC 下编译:如何获得更好的调试信息
- gcc 可以使用较旧的第三方库编译 C++17 代码吗?
- 如何使用 gcc 内联汇编器代码访问成员变量
- 初始化不可移动对象数组:为什么这样的代码无法在 GCC 上编译?
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- variadic模板代码中的GCC VS MSVC编译误差
- Clang vs GCC:挥发性访问的不同代码
- 编译为C 与C的GCC代码生成的很大差异
- 在 Visual Studio 中编译 gcc 代码会导致错误 C3646:"__attribute__":未知的覆盖说明符
- 编译带有AVX矢量警告的遗留GCC代码
- c++ stl映射编译问题与GCC/代码块
- 需要连接std:string + WCHAR移动gcc代码到Visual c++ 2010