Reputation: 327
I am learning class inheritance, and I am struggling to figure out a small problem. I have two classes
class OrderList {
private:
std::vector<AbstractClass*> elements;
public:
OrderList(const std::vector<AbstractClass*>& list){
// for (auto &i : list)
// elements.push_back(i);
}
OrderList(const OrderList& ol);
OrderList& operator=(const OrderList&);
virtual ~OrderList(){};
};
class DerivedOrderList : public OrderList {
DerivedOrderList(std::vector<AbstractClass*>& list) : OrderList(list){}
DerivedOrderList(const DerivedOrderList&);
DerivedOrderList& operator=(const DerivedOrderList&);
virtual ~DerivedOrderList(){};
};
Then, my main function calls on DerivedOrderList as such:
DerivedOrderList myOrderList = DerivedOrderList(myVector);
I am getting an error: No instance of constructor "DerivedOrderList::DerivedOrderList" that matches the arguments
What might be causing this?
Upvotes: 0
Views: 6821
Reputation: 454
std::vector<AbstractClass*>
is not the same as std::vector<DerivedClass*>
. The compiler creates a completely different class for each separate template implementation.
Why don't you just provide overloaded constructors DerivedOrderList::DerivedOrderList(std::vector<DerivedClass*>&)
and OrderList::OrderList(std::vector<DerivedClass*>&)
?
Upvotes: 1
Reputation: 651
Constructor in DerivedOrderList needs to be public.
Edit: Also see NathanOliver's comment and Rob K's answer regarding type of myVector.
Upvotes: 3
Reputation: 8926
OrderList(const std::vector<AbstractClass*>& list)
takes a const reference.
DerivedOrderList(std::vector<AbstractClass*>& list)
takes a mutable reference. Change it to DerivedOrderList(const std::vector<AbstractClass*>& list)
Also, I'd recommend typdef
ing your vector<AbstractClass*>
somewhere. You use it in a lot of places, so Don't Repeat Yourself.
ETA: Also this DerivedOrderList myOrderList = DerivedOrderList(myVector);
should be DerivedOrderList myOrderList( myVector );
This is not Java or C#.
Upvotes: 3