It is possible somehow through * this, but I don’t understand how much, and I don’t know where to put it in

friend day operator+=(day& dy, product& prod) { dy.ygl+= prod.Yglevod; dy.bel+= prod.Belok; dy.gur+= prod.Gur; dy.kkal+= prod.Kkal; return dy; } 

this is class action

product is another class

    2 answers 2

    Your operator is described as a free function; In my opinion, you did not want this. As I understand it, this is an operator for day - then it should be in the class day and look something like this -

     day& operator+=(const product& prod) { ygl+= prod.Yglevod; bel+= prod.Belok; gur+= prod.Gur; kkal+= prod.Kkal; return *this; } 

    For such an access to the closed members of product your day class must be declared a friend; well, or you can declare as a friend only the += operator from the day class.

    PS The operator can be declared as a free function, but, in my opinion, for the += operator it is more logical to be declared within the class as a member function.

    PPS Here is a simple example where operators are declared both in the class and beyond.

     class A; class B { public: B& operator +=(const A& a); int y; }; class A { private: int x; friend B& B::operator +=(const A& a); friend B& operator -=(B& b, A& a); }; B& B::operator +=(const A& a) { y += ax; return *this; } B& operator -=(B& b, A& a) { by -= ax; return b; } int main(int argc, const char * argv[]) { B b; A a; b += a; B c = b -= a; } 
    • understood, recently I study, for the first time I encountered this. Now I’ll go look for friendly classes. Thank you very much!! - Max
    • I am there to help you with a small source in the answer added, look. - Harry
    • had seen. Very helpful, thanks! - Max
    • Well, if you are satisfied with the answer, mark it as accepted :) - Harry
    • still fails. I need to create this method in the classroom: day& operator+=(const product& prod) { ygl+= prod.Yglevod; bel+= prod.Belok; gur+= prod.Gur; kkal+= prod.Kkal; return *this; } day& operator+=(const product& prod) { ygl+= prod.Yglevod; bel+= prod.Belok; gur+= prod.Gur; kkal+= prod.Kkal; return *this; } day& operator+=(const product& prod) { ygl+= prod.Yglevod; bel+= prod.Belok; gur+= prod.Gur; kkal+= prod.Kkal; return *this; } and in the classroom product make this method friendly. type friend day& day::operator+=(const product& prod); So? - Max

    Declare operator friendly function for both classes. The following shows how to do it.

     #include <iostream> class B; class A { public: friend A & operator +=(A &a, const B &b); explicit A(int x = 0) : x(x) {} std::ostream & out(std::ostream &os = std::cout) const { return os << x; } private: int x; }; class B { public: friend A & operator +=(A &a, const B &b); explicit B(int y = 0) : y(y) {} std::ostream & out(std::ostream &os = std::cout) const { return os << y; } private: int y; }; A & operator +=(A &a, const B &b) { ax += by; return a; } int main() { A a(10); (a += B(20)).out() << std::endl; } 

    You can also include an operator for the right operand rvalue

     friend A & operator +=(A &a, B &&b); 

    An alternative approach is to declare this operator in one class as a member of a class, and in another class as a friend function.