gator
gator

Reputation: 3523

Implicit assignment operator deleted for vector erasing?

I have two vectors and I wish to compare them and delete duplicates. If I had two vectors vecA and vecB, I want to remove elements from vecA if they appear in vecB. Specifically, I want vecA to be passed to a method and mutated within that method.

#include <iostream>
#include <string>
#include <vector>

class M {
    private:
        const char COL[8] = {'a','b','c','d','e','f','g','h'};
    public:
        int aC, aR, dC, dR;
        M(int a, int b, int c, int d) {
            aC = a; aR = b; dC = c; dR = d;
        }
        bool operator==(M &m) {
            bool sameC = (aC == m.aC && dC == m.dC);
            bool sameR = (aR == m.aR && dR == m.dR);
            return (sameC && sameR);
        }
};

class B {
    public:
        std::vector<M> foo() {
            std::vector<M> vec;
            vec.push_back(M(1, 3, 2, 4));
            vec.push_back(M(1, 2, 3, 4));
            return vec;
        }
};

class K {
    public:
        void boo(B* b) {
            std::vector<M> vec;
            vec.push_back(M(1, 2, 3, 4));
            vec.push_back(M(2, 3, 4, 5));
            std::cout << "Size before: " << vec.size() << "\n";
            bar(b, vec);
            std::cout << "Size after: " << vec.size() << "\n";
        }
        void bar(B* b, std::vector<M> &v) {
            std::vector<M> vec = b->foo();
            for (unsigned int i = 0; i < v.size(); i++) {
                for (unsigned int j = 0; j < vec.size(); j++) {
                    if (v[i] == vec[j]) { v.erase(v.begin() + i); }
                }
            }
        }
};

int main()
{
    B* baz;
    K var;
    var.boo(baz);
}

I get an error that M& operator=(const M&) is implicitly deleted because the default definition would be ill-formed.

The problem code is that const char array; removing this bit of code, everything else works fine but why?

An expected output for this code would be:

Size before: 2
Size after: 1

Why is an assignment operator needed for the iteration anyways (for erase) and is there a workaround? What would the "default assignment operator" be in this case?

Upvotes: 1

Views: 518

Answers (1)

Mooing Duck
Mooing Duck

Reputation: 66922

class M has a non-static member variable COL that is an array of const things, so the compiler can't copy the contents of the array over.

Presumably you meant to make COL static, so it isn't a subobject of each M object?

Upvotes: 2

Related Questions