#pragma once #include "iostream" #include "conio.h" #include "fstream" class product { private: char* Name; double Kkal; double Belok; double Yglevod; double Gur; public: product() { Kkal = Belok = Yglevod = Gur = 0; Name= ""; } product(double bel, double ygl, double gur, char* name) { Belok= bel; Yglevod= ygl; Gur= gur; Kkal= (bel*4)+(ygl*4)+(gur*9); Name= name; } friend std::ostream& operator<<(std::ostream& os,const product p1) { os<<p1.Name<<p1.Belok<<" белка, "<<p1.Gur<<" жира, "<<p1.Yglevod<<" углеводов, "<<p1.Kkal<<"ккал "; return os; } friend std::ofstream& operator<<(std::ofstream& fout, const product& p1) { fout.open("product.txt",std::ios::app); fout << p1.Name <<std::endl; fout << p1.Belok << ' ' << p1.Gur << ' ' << p1.Yglevod << ' ' << p1.Kkal<< std::endl; return fout; } friend std::ifstream& operator>>(std::ifstream& fin, product& p1) { fin.getline(p1.Name, 30); fin >> p1.Belok; fin >> p1.Gur; fin >> p1.Yglevod; (fin >> p1.Kkal).get(); return fin; } product operator+(const product& a) const { product summa; summa.Belok= Belok+a.Belok; summa.Gur= Gur+a.Gur; summa.Yglevod= Yglevod+a.Yglevod; summa.Kkal= Kkal+a.Kkal; return summa; } }; 

 friend std::ifstream& operator>>(std::ifstream& fin, product& p1) 

does not work correctly

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

You need to allocate memory where you are going to read the character value for the Name field.

 fin.getline(p1.Name, 30); 

Your Name field is declared as a pointer, not as an array.

If you declare these fields to be of type std::string , then the class definition will be much easier.

 class product { private: std::string Name; //... 
  • Thank you very much, but how then in the designers to fill it? via strcpy ()? - Max
  • @ Max If you use the class std :: string, then simply assign a string. - Vlad from Moscow
  • with string it will be possible instead of fin.getline (p1.Name, 30); make fin >> p1.Name; ?? or withdraw - Max