Hello,

I am writing a program whose source code can be viewed and compiled here. http://goo.gl/InB8B6 . If you try to compile it by reference, you will get an error.

main.cpp: In function 'int main ()':
main.cpp: 42: 8: error: cannot declare variable 'sh' to be of abstract type 'Shape'
Shape sh;

Some details:

  • Shape abstract shape class
  • Canvas class canvas. The add() method returns a pointer to the shape. And then you can perform methods on this figure. For example: shape->draw() , shape->hide()

Please help me figure out how to fix the code so that:

  1. You could create an object of class Shape
  2. It was possible to correctly execute Canvas::add() and remember the pointer to the figure

For convenience, here are the code snippets:

Shape.h

 #pragma once using namespace std; class Shape { public: virtual void draw() = 0; virtual void hide() = 0; virtual ~Shape() {} }; 

Line.h (one of the heirs of Shape)

 #pragma once #include <iostream> #include "Point.h" #include <math.h> #include "Shape.h" using namespace std; class Line : public Shape { public: Line(); Line(Point a, Point b); virtual void draw(); virtual void hide(); void move(Point da, Point db); void setA(Point a); Point getA(); void setB(Point B); Point getB(); float len(); protected: Point a; Point b; }; Line::Line() { Point a, b; Line(a, b); } Line::Line(Point a, Point b) { this->a = a; this->b = b; } void Line::draw() { cout << "      "<< a.print() <<"-"<< b.print() << "           " << endl; } void Line::hide() { cout << "      " << a.print() << "-" << b.print() << "       " << endl; } void Line::move(Point da, Point db) { this->hide(); a += da; b += db; this->draw(); } // ... 

main.cpp

 #include <iostream> #include "Point.h" #include "Line.h" #include "Triangle.h" #include "Quadrangle.h" #include "Pentagon.h" #include "Ellipse.h" #include "Circle.h" #include "Canvas.h" int main() { setlocale(LC_ALL, ""); Point c2a(0, 0); Point c2b(1, 0); Point c2c(1, 2); Point c2d(0, 2); Point c2e(1, 0); Canvas cnv; Shape sh; //sh = cnv.add(c2a); //sh->draw(); return 0; } 
  • I will answer the first question - you cannot create an abstract class object. ru.wikipedia.org/wiki/Abstract_class - A1essandro
  • @ A1essandro, I attached the code to the question. I hope that I described the idea quite clearly. Can you tell me how to do this correctly? I could answer the questions if something is not clear. - ssh
  • @ssh sorry for an indiscreet question, did you write this code yourself? - DreamChild
  • @DreamChild, yes - ssh
  • one
    @ssh then the recipe is simple - use pointers to classes. However, @ fori1ton has already explained everything to a lot more - DreamChild

1 answer 1

Line

 Shape sh; 

is an implicit creation of an object of the Shape class by calling its default constructor. But the Shape class is abstract, so an attempt to create an instance of it will result in a compilation error. If you do not want the object to be created immediately upon declaration, use pointers instead of links:

 Shape* sh; sh = cnv.add(c2a); sh->draw(); 

Now the implicit creation of the object does not occur, instead it is assigned a pointer to a non-abstract class that inherits the pointer to the abstract parent class, which is a correct code.

Read more about the differences between links and pointers in C ++.