0

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

5
  • 8
    Your first issue is deriving from std::vector... do not inherit from types that were not designed to be inherited from. Instead use composition (i.e. store a std::vector internally). Jun 15, 2012 at 15:06
  • 4
    You should make operator== and operator!= const-correct. (i.e. bool operator == (const Item& rhs) const { ...)
    – Cornstalks
    Jun 15, 2012 at 15:07
  • Besides theusual inheriting from vector issues, does the error tell you what is expected on the RHS? And does it give any close matches? Jun 15, 2012 at 15:10
  • It sounds like you're deriving from vector to enforce some constraints on Item. Is there any reasonable possibility of Item enforcing those constraints itself? Jun 15, 2012 at 15:41
  • I mentioned that I've seen the arguments for/against deriving from std::vector<>. (Some of the arguments made in the "for" camp came from some threads here at Stack Overflow.) The list class I made shouldn't be inherited from, so I didn't think the lack of virtual desctructors in std::vector<> would be a problem. @Jerry I hadn't considered your comment before. I've got to think about it more, but I think I might just be able to add the constraints in class ctor. Jun 15, 2012 at 17:15

1 Answer 1

1

The proper overload for == is

class Item
{
    ...
    bool operator==(const Item& rhs) const
    { .... }

    bool operator!=(const Item& rhs) const
    { return !(*this==rhs); }
};

Also, be shure that, because std::vector doesn't have virtual members, your derived ItelmList cannot be used polymorphically against std::vector itself, 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)

3
  • 1
    You've never seen a std::vector* or std::string*? Clearly you've not spent any time reading noob C++ questions here on SO ;-)
    – Rook
    Jun 15, 2012 at 15:41
  • @Rook: :-))) +1 ! -BTW- It's normal for noobs doing mistakes. What isn't, is protecting against them. After I learnt the difference between == and = I stopped coding "yoda comparisons" (like if(0==a)...). After I learnt about the difference between virtual and not, I stopped dooming non-virtual dtors. Jun 15, 2012 at 15:46
  • Looks like I had the const problems. I've recoded what I have follow this pattern and things built. Still considering the wisdom of this though. Although my code now builds, I'm not sure it will remain like this, given the very serious considerations of deriving from std::vector<>. This class should never be derived from. Then again, I was told I'd never need more than 640k of RAM also. Jun 15, 2012 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.