There is a parent class with operator overload +

class Figure { public: static sf::RenderWindow *windluz; static int N; static const int MAX; sf::Vector2f position, size; sf::Color color; virtual double square() {}; virtual void draw() {}; virtual void move(sf::Vector2f shift) {}; virtual void scale(double zoom) {}; Figure(sf::Vector2f position, sf::Vector2f size, sf::Color color); friend Figure * operator+(Figure *a, Figure b) { a = &b; Figure::N++; return a; } }; 

Child class

 class Rectangle : public Figure { public: sf::RectangleShape rectangleShape; Rectangle(sf::Vector2f position, sf::Vector2f size, sf::Color color); void draw(); void move(sf::Vector2f shift); void scale(double zoom); double square(); double square(double a, double b); }; 

In the main file

 sf::RenderWindow *Figure::windluz; int Figure::N = 0; const int Figure::MAX = 10; Figure *array[Figure::MAX]; 

and in the main function

 sf::RenderWindow window(sf::VideoMode(1920, 1080), "classes"); Figure::windluz = &window; Rectangle rectangle(sf::Vector2f(60, 120), sf::Vector2f(60, 80), sf::Color::Black); array[Figure::N] + rectangle; 

an attempt to call a method from an array element causes a segmentation fault error.

 for (int i = 0; i < Figure::N; ++i) { array[i]->draw(); //segmentation fault } 

I suspect that the problem is in improper operator overloading, because if you write an element to an array directly, then everything is fine

 array[Figure::N] = &rectangle;//работает 

I need to write an element to an array using "+"

  • Understood nothing. So where is any meaningful initialization of the elements of the array array ? Null initially contains null pointers. array[Figure::N] + rectangle does not affect the value of the array element, that is, null pointers remain there. It is clear that when you call everything will fall. How was this supposed to work? - AnT pm
  • And what is "write an element into an array using +"? - AnT
  • @AnT into an array using the + operator like this: "array [Figure :: N] + rectangle;" A link must be added to the new element, so that later it would be possible to walk through the array and call the draw method of any child class. - super sahar
  • @AnT I do not understand how to initialize this array. - super sahar

1 answer 1

What do you have with classes? Look at them: you use pointers in classes, but neither destructors nor copy constructors are visible. When you destroy an object, the memory will not be freed, and when copying, the pointer will simply zadvoitsya, and useful information will not be copied.

Next: what is this code in general?

 friend Figure * operator+(Figure *a, Figure b) { a = &b; Figure::N++; return a; } 

You pass to the function Figure b by value (copy the object — but you don’t have a copy constructor!), And then you take its address, assign it to a and return it ... What is this pornography? In order to do this, you had to pass b not by value, but by address.

Next: what does it mean

 Figure *array[Figure::MAX]; 

Are you aware that you created an array of pointers? Ie in each element of the array not an object, but a pointer to it. Is initialization occurring? I think that you have not taken care of this. But ok, let's do this:

 Figure array[Figure::MAX]; 

Then the elements will be objects, not pointers to them. But you have not taken care of the default constructor. And in general, what kind of classes do you have? Why methods along with variables in public ? Classes were not created for this purpose in order to treat them like this. Data must be hidden and given access only through appropriate methods. Constructors should take care of initialization, and destructors take care of freeing memory. What you do (do not care about initialization, saving and proper use of data) just leads to such errors.

  • What I need is an array of pointers into which, using the operator + overload, I need to add an object reference - super sahar
  • In this case, look at what I wrote about your version of the operation + . - Andrej Levkovitch