我试图擦除存储包含const成员的类的向量的元素,但失败并出现错误。
当我删除常量修饰符时,程序将正常编译。
class C
{
public:
const int i;
C(int i = 1):i(i){};
};
int main()
{
vector<C> v;
C c;
v.push_back(c);
v.erase(v.begin());
return 0;
}
error C2280: 'C &C::operator =(const C &)': attempting to reference a deleted function
m.cpp(151): note: compiler has generated 'C::operator =' here
m.cpp(151): note: 'C &C::operator =(const C &)': function was implicitly deleted because 'C' has a data member 'C::i' of const-qualified non-class type
m.cpp(146): note: see declaration of 'C::i'