mercredi 31 août 2016

vector - swap two elements when elements have const members

I have the following code

class a {
public:
    const int aa;
    a(int aa) : aa(aa){}
};
int main() {
    std::vector<a> v;
    v.emplace_back(1);
    v.emplace_back(2);
    v.emplace_back(3);
    v.emplace_back(4);

    std::iter_swap(v.begin() + 1, v.rbegin());

    system("pause");
    return 0;
}

I get an error when I try to swap two elements of the vector.

Error   C2280   'a &a::operator =(const a &)': attempting to reference a deleted function

I understand it's because a has a constant member, but I am not able to figure out how to get this to work.

Aucun commentaire:

Enregistrer un commentaire