I have to create the instance of class which inherits from abstract class. My code is really simple. It should create the instance of object class based on abstract class. The abstract class is template class aswell. Then I need to put this object into storage class which holds pointer to the object. Thats all. This task is some kind of homework.
Is that even possible to create the instance of class based on abstract class?
If yes - what I am doing wrong? If no - how can I make it similar?
#include <iostream> #include <string> #include <memory> // using namespace std; template<typename type1, typename type2, typename type3> class INTERFACE { protected: type1 x; type2 y; type3 name; public: virtual type1 setX() = 0; virtual type2 setY() = 0; }; class child : public INTERFACE<int, float, std::string> { public: child(std::string z) { this->name = z; } int setX(int x) { this->x = x; } float setY(float y) { this->y = y; } }; class storage { private: std::shared_ptr<child> childPTR; public: void setPTR(const std::shared_ptr<child> & pointer) { this->childPTR = pointer; } }; int main(){ std::shared_ptr<child> newChild(new child("xxx")); storage testStorage; testStorage.setPTR(newChild); return 0; } Compiling Errors:
templates.cpp: In function ‘int main()’: templates.cpp:44:52: error: invalid new-expression of abstract class type ‘child’ std::shared_ptr<child> newChild(new child("xxx")); ^ templates.cpp:18:7: note: because the following virtual functions are pure within ‘child’: class child : public INTERFACE<int, float, std::string> { ^ templates.cpp:14:23: note: type1 INTERFACE<type1, type2, type3>::setX() [with type1 = int; type2 = float; type3 = std::__cxx11::basic_string<char>] virtual type1 setX() = 0; ^ templates.cpp:15:23: note: type2 INTERFACE<type1, type2, type3>::setY() [with type1 = int; type2 = float; type3 = std::__cxx11::basic_string<char>] virtual type2 setY() = 0;
overridespecifier. You'll find it illuminating. In particular,()and(int x)do not declare the same parameter list.Instance of class which inherits from abstract class