Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Inheritence and Abstract classes in vectors
I'm new to C++ and learning the ropes (coming from a Java background). I've tried to have a vector of abstract classes but the compiler chokes if i try and call the push_back method.
Heres what I'm doing (in simple terms), I have an abstract Account class (there are many different types of accounts but all have the same properties) which i need to store as a vector (as opposed to an array purely because our collection grows/shrinks) now I'm using several (pure) virtual functions in the Account.hpp file so it is an abstract class. Two implemented classes TaxAccount and SalesAccount are created and everything seems to work, unfortunately I cant add any instances of TaxAccount or Sales account to the vector of pointers.
Declared the abstract class and have this in my header:
Adding it like so in accountHandler.cpp:
void AccountHandler::addAccount(const Account* item)
Error 2 error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'const Account *' to 'Account *const &' x:UsersEmilyGDSBAAccountHandler.cpp 15