[英]Error when using the std::allocator in the template class
Trying to figure out how to use std :: allocator. 试图弄清楚如何使用std :: allocator。
#include <iostream>
template <typename T, typename A>
struct vector_base
{
A allocator;
T* data;
size_t size;
size_t space;
vector_base(const A &alloc, size_t n)
:allocator(alloc), data(alloc.allocate(n)), size(n), space(n)
{}
~vector_base() {allocator.deallocate(data, space);}
};
int main() {
std::allocator<int> my_allocator;
vector_base<int, std::allocator<int>> vector(my_allocator, 10);
return 0;
}
Error: 错误:
error: passing 'const std::allocator' as 'this' argument discards qualifiers [-fpermissive] :allocator(alloc), data(alloc.allocate(n)), size(n), space(n)
错误:将“ const std :: allocator”作为“ this”参数传递会丢弃限定符[-fpermissive]:allocator(alloc),data(alloc.allocate(n)),size(n),space(n)
alloc
is a const&
, you're trying to call allocate
on it which is a non-const method, that's not allowed. alloc
是一个const&
,您试图在其上调用allocate
,这是一个非const方法,这是不允许的。
Call allocate
on your initialized member instead: 而是在初始化的成员上调用
allocate
:
vector_base(const A &alloc, size_t n)
:allocator(alloc), data(allocator.allocate(n)), size(n), space(n)
{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.