My compiler is outputting the following error: "cast from base to derived requires dynamic_cast or static_cast" even though im declaring a dynamic cast about 2 lines before. Here's my basic setup
"error: '=' cannot convert from 'Shape *' to 'Triangle *' Cast from base to derived requires dynamic_cast or static_cast"Code:class Shape { public: Shape(){}; virtual ~Shape(){}; }; //above format for the following classes class Rectangle:public Shape class Square:public Rectangle class Triangle:public Shape class Equilateral:public Triangle class Unrelated: { public: Unrelated(){}; ~Unrelated(){}; Triangle * atriangle; std::vector<Shape*> vectorofshapes; void InputShape(Shape * pShape){vectorofshapes.push_back(pShape);} void FetchShape(Shape * ppShape) { Triangle * testtriangle = dynamic_cast<Triangle*>(ppshape); if (testtriangle != NULL) atriangle = ppshape; } } int main() { Unrelated object; Triangle * triangle[5]; for ( i = 0; i < 4; i++ ) triangle[i] = new Equilateral; object.InputShape(triangle[0]); object.FetchShape( object.vectorofshapes.at(0) ); return 0; }
On a side note the main reason for my equilateral and square classes is so that i can produce copies of specific types of my triangle and rectangle classes during runtime...they don't have any different properties, just more specific. Like in the above example, let's say a constructor for Triangle was Triangle(int a,b,c){angleA = a, angleB = b, angleC = c}, the only difference between Triangle and Equilateral would be that Equilateral's constructor is Equilateral(){}:Triangle(60,60,60); So if there is a more efficient way to do this other than creating whole new derived classes that would definately be preferred.