mercredi 2 mars 2016

Class containing vector of classes

I have defined two classes :

class Group
{
  public:
    Group ( void );
    bool addStudent ( const Student & X );
    void printAll( void ) const;

  protected:
    vector<Student> vectorOfStudents;
};
//-----------------------------------------------
class Student
{
   public:
     Student ( string name, int age );
     void printAtributes ( void );

   protected:
     string                    nameOfStudent;
     int                       ageOfStudent;
};

I'm creating objects and storing them in Group object to vector but i have problem when i want to print them :

void Group::printAll ( void ) const
{
  for ( const auto &  student : vectorOfStudents )
  {
    student . printAtributes (  ); // Line 54
    cout << endl;
  }
}

Here is the function which should call and print name of student every time :

void Student::printAtributes ( void )
{
  cout << "name: " << nameOfStudent << " | " << "age: " << ageOfStudent<< endl;
}

Gives me this error :

54:33: error: passing ‘const Student’ as ‘this’ argument of ‘void Student::printAtributes()’ discards qualifiers [-fpermissive]
     student . printAtributes (  );

Aucun commentaire:

Enregistrer un commentaire