I have a class
class Item {
int _one;
int _two;
int _three;
// other stuff
};
class ItemList : public std::vector<Item> {
// deriving from std vector because the ctor needs to
// perform some work in discriminating what gets added
// to the list
};
I’ve read many of the arguments against deriving from std::vector<> and I think I’ll be ok because this class isn’t to be derived from. I’m exposing this in python, using the boost vector indexing suite, to act as a python list. Because I needed the constructor to do some work in eliminating certain elements from the list during construction, I decided to go this route instead of doing what I’ve done elsewhere in the project:
class AnotherItem {
// class definition
};
typedef std::vector<AnotherItem> AnotherItemList
And then expose the list with boost vector indexing suite using the typedef. All seems well except that I have this error: Error 2 error C2678: binary ‘==’ : no operator found which takes a left-hand operand of type ‘Item’ (or there is no acceptable conversion)
The error isn’t coming from the boost libraries but something in the std algorithm code. I’ve tried to added my own class overloaded == operator, but this hasn’t fixed the problem. It looks like this:
class Item {
// earlier stuff
bool operator==(Item& rhs) {
return (_one == rhs._one && _two == rhs._two && _three == rhs._three);
}
bool operator!=(Item& rhs) {
return !(*this == rhs);
}
};
This hasn’t fixed the issue. What am I missing? This link here shows that the == operator for vector is NOT a member function. I tried overloading at the “global” level (I.e. not within a namespace), but this didn’t help either. So, what am I missing?
Thanks,
Andy
The proper overload for == is
Also, be shure that, because
std::vectordoesn’t have virtual members, your derivedItelmListcannot be used polymorphically againststd::vectoritself, in particular don’t call delete against an std::vector*.I have to say this because otherwise me and you will be doomed by tha C++ communinty although in 30+ year of experience in programming I never never never saw a std::vector* or an std::string*. (And hence I really don’t know all the “scare” about deriving std classes is about: simply know what you are doing and let other people know)