This is my student class.

#define STUDENT_H #include <string> #include <fstream> using namespace std; class Student { public: Student(); Student(string name, string surname, int age, int marks[]); void setStudent(string name,string surname,int age,int marks[]); string getName(); string getSurname(); int getAge(); int getMarks(); void printStudent(); void writeStudent(ofstream &fout); private: string name; string surname; int age; int marks[5]; string subjects[5]; }; 

Here is its implementation

 #include "Student.h" #include <iostream> #include <fstream> using namespace std; Student::Student() { subjects[0]="algebra"; subjects[1]="art"; subjects[2]="chemistry"; subjects[3]="geometry"; subjects[4]="physics"; } Student::Student(string name, string surname, int age, int marks[]) { this->name=name; this->surname=surname; this->age=age; for(int i=0; i<5; i++){ this->marks[i]=marks[i]; } } void Student::setStudent(string name,string surname,int age,int marks[]){ this->name=name; this->surname=surname; this->age=age; for(int i=0; i<5; i++){ this->marks[i]=marks[i]; } } string Student::getName() { return name; } string Student::getSurname(){ return surname; } int Student::getAge(){ return age; } int Student::getMarks(){ for(int i=0; i<5; i++){ return marks[i]; } } void Student::printStudent(){ cout<<"Name: "<<getName()<<endl; cout<<"Surname: "<<getSurname()<<endl; cout<<"Age: "<<getAge()<<endl; for(int i=0;i<5;i++) { cout<<subjects[i]<<"="<<((getMarks()>=0)?getMarks():0)<<((i!=4)?", ":""); } cout<<endl; } void Student::writeStudent(ofstream &fout){ fout<<getName()<<endl; fout<<getSurname()<<endl; fout<<getAge()<<endl; for(int i=0;i<5;i++) fout<<getMarks()<<" "; fout<<endl; } 

I want to create functions for writing and reading to a file. Started with file write function: writeStudent(ofstream &fout) . But when doing it gives an error

no 'void Student :: writeStudent (std :: ofstream &)' member function.

I can not fix it.

  • I have your code compiled and the writeStudent function without errors is called in Maine, the g ++ 4.8 compiler - Shadasviar
  • @ Vugar Eyvazov You included Student.h in the main? - Vlad from Moscow
  • Tried to do a full cleanup and reassembly of the project? - Shadasviar
  • @VladfromMoscow, apparently, yes, since the compiler knows about the Student class. - isnullxbh

1 answer 1

Instead of #define STUDENT_H -> #pragma once

This function will execute a return at the first iteration and will not be executed further:

 int Student::getMarks(){ for(int i=0; i<5; i++){ return marks[i]; } } 

These functions can be made const and return std :: string const &:

 string getName() -> string const& getName() const; string getSurname() -> string const& getSurname() const; 

Similarly, the above can be applied to the input parameters in the constructor.

Since you have already included the #include <string> and #include <fstream> in the .h file, you do not need to do this in the .cpp file.

  • I did everything you said and sort of earned the Student.cpp file. - Vuqar Eyvazov
  • But how to add the writeStudent () function to the main file. He gives an error there [Error] - Vuqar Eyvazov
  • @ VugarAyvazov, but show the code in main? - isnullxbh
  • Below added main code - Vuqar Eyvazov