C++ 请求成员‘;插入’;在‘;x’;,它是非类类型的
我有一个称为x的映射变量,其定义如下:C++ 请求成员‘;插入’;在‘;x’;,它是非类类型的,c++,map,stl,allocator,C++,Map,Stl,Allocator,我有一个称为x的映射变量,其定义如下: std::map <std::string, std::string, std::less<std::string>, MyAlloc<std::pair <std::string, std::string> > > x (std::less<std::string> (), MyAlloc <std::pair <std::string, std::strin
std::map <std::string, std::string, std::less<std::string>,
MyAlloc<std::pair <std::string, std::string> > >
x (std::less<std::string> (), MyAlloc <std::pair <std::string, std::string> > ());
std::map
x(std::less(),MyAlloc());
其中MyAlloc是我定义的自定义STL分配器。当我尝试在地图中插入一对时,如:
x.insert(std::pair<std::string,std::string>("key","value"));
x.insert(标准::对(“键”、“值”);
我得到以下错误:
error: request for member ‘insert’ in ‘x’, which is of non-class type
‘std::map<std::basic_string<char>, std::basic_string<char>,
std::less<std::basic_string<char> >, MyAlloc<std::pair<std::basic_string<char>,
std::basic_string<char> > > >(std::less<std::basic_string<char> > (*)(),
MyAlloc<std::pair<std::basic_string<char>, std::basic_string<char> > > (*)())’
错误:请求在非类类型的“x”中插入成员
'std::map(std::less(*)(),
MyAlloc(*)()'
如果使用分配器的默认构造函数,则不会出现此错误,如:
std::map <std::string, std::string, std::less<std::string>,MyAlloc <std::pair <std::string, std::string> > > x;
std::map x;
我不确定默认构造函数的初始化是否正确
提前感谢……) 您定义的是函数
x
,而不是对象
默认的ctor将使用相同的比较和分配器,所以使用它。这里要查找的关键字是“最麻烦的解析”。
x
的定义被解析为函数声明。它提到了完全相同的错误消息。事实上,我想在分配器工作后使用一个自定义构造函数,类似这样:std::map x(std::less(),MyAlloc(d));我的自定义分配器中有一个自定义构造函数,它可以采用d的类型我有点困惑。。x怎么可能是一个函数。。?它是一个std::map类型的对象,但只是它使用自定义stl分配器而不是默认构造函数。@codinonwheels:为什么您认为它是一个对象?它的定义是返回类型x(argumentType1,argumentType2)
。但一旦使用表达式(d)
,它就不再是函数定义。函数定义只包含作为默认参数的表达式。确切地说,我错过了它。。完全。。!非常感谢……)