我正在尝试创建一个不可变“Entry”对象的向量。
#include <vector>
class Entry{
private:
const double mLimit;
const int mQty;
public:
Entry(const double limit, const int qty) : mLimit(limit), mQty(qty) {}
};
int main(){
std::vector<Entry> entries;
entries.push_back(Entry(1.9, 3));
return 0;
}
这是编译器的输出:
>>> g++ test.cpp && ./a.out
test.cpp: In instantiation of ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(std::vector<_Tp, _Alloc>::iterator, const _Tp&) [with _Tp = Entry; _Alloc = std::allocator<Entry>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<Entry*, std::vector<Entry> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = Entry*]’:
/usr/include/c++/5/bits/stl_vector.h:925:17: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Entry; _Alloc = std::allocator<Entry>; std::vector<_Tp, _Alloc>::value_type = Entry]’
test.cpp:15:36: required from here
test.cpp:4:7: error: non-static const member ‘const double Entry::mLimit’, can’t use default assignment operator
class Entry{
^
test.cpp:4:7: error: non-static const member ‘const int Entry::mQty’, can’t use default assignment operator
In file included from /usr/include/c++/5/vector:69:0,
from test.cpp:2:
/usr/include/c++/5/bits/vector.tcc:343:16: note: synthesized method ‘Entry& Entry::operator=(const Entry&)’ first required here
*__position = __x_copy;
这似乎是一个简单的错误。任何帮助将不胜感激。