There are two structures that implement the geometric concepts of a point and a vector. To initialize the vector requires two points. Similarly, there is an operation of addition of a point and a vector through operator + overload. The compiler curses the absence of a Vector structure in overload, which is understandable, since it is implemented after. How to pre-declare a Vector structure. (struct Vector;) does not work. Or here is the error of the ad logic itself.

struct Vector; struct Point { int X; int Y; [***] Point operator + (const Vector& ab) { this->X += ab.X; this->Y += ab.Y; return *this; } }; struct Vector { int X; int Y; [***] Vector(Point& a, Point& b) { X = bX - aX; Y = bY - aY; } }; 

    3 answers 3

    With the logic of the preliminary announcement, everything is in order. Simply defining your operator should be done where the definitions of both classes are already fully known . Those. not inside the first class, but outside - after both class definitions, like inline

     inline Point Point::operator + (const Vector& ab) { this->X += ab.X; this->Y += ab.Y; return *this; } 

    Inside the first class there should be only an operator declaration.

    However, you have realized something strange. The binary operator + , which suddenly for no reason at all spoils (modifies) its first operand, is terrible. Why did you need such a strange operator?

    • Indeed it is terrible, the error of implementation, already fixed on the return of a new object, thank you. In the process of studying the overload of C ++ operators. - Khetag Abramov

    Take out the definition of an operator outside the class, after the declarations of both classes. In the class, leave only the statement operator, no body. Like that:

     struct Vector; struct Point { int X; int Y; [***] Point operator + (const Vector& ab); }; struct Vector { int X; int Y; [***] Vector(Point& a, Point& b) { X = bX - aX; Y = bY - aY; } }; Point Point::operator + (const Vector& ab); { X += ab.X; Y += ab.Y; return *this; } 

      The problem is that in order to place an object in a class, the compiler needs to know its size. Therefore, you cannot stuff an object that has not yet been described into your class — no way ... except for a pointer or a link. The fact is that their sizes are always the same, regardless of the objects to which they point. As for specifying the class name, to declare a pointer (or a link) to it, just write

       class my_class; 

      in front of the class in which you will place the pointer to my_class .

      UPD speaking class - I meant the concept in general, and not specifically the class