C++ Cannot allocate an object of abstract type -
i'm new c++ , know error about. code below.
questions:
- i read in post use pointer abstract base class, how do without dynamic allocation?
- can using reference instead? tried, didn't work.
- can use union {circle c, shape s};? tried, didn't work.
in examples below, circle , square inherit abstract base class shape.
int main() { std::vector<shape> shapes; //error! circle c (5); square s(4); shapes.push_back(c); shapes.push_back(s); return 0; }
apparently have defined type shape abstract type circle , square types derived shape.
what is;
std::vector<shape*> shapes ;
and store porters squares , circles shapes vector.
shapes.push_back (&c) ; shapes.push_back (&s) ;
Comments
Post a Comment