简体   繁体   中英

Vector storing objects

I have created 3 classes.

  • All class - storing all Groups
  • Group class - Storing objects of students into vector.
  • Studen class - storing information about a student.

I have a problem with All class - method printAllofThem it should iterate through Group objets and call method in printAll which should iterate though all Student objects and call printAtributes, but the output looks like this :

all . printAllofThem ( );

Output:

-------------------
-------------------

-------------------

Expected output:

-------------------
name: Mark | age: 20 | A1

name: Alan | age: 20 | A1

name: Eric | age: 19 | A1

-------------------
name: John | age: 19 | B1

It gives me the right output only when i call it like this in main function:

A1 . printAll  ( );
B1 . printAll  ( );

Code :

#include <string>
#include <iostream>
#include <vector>

using namespace std;

class Student
{
  public:
    Student ( string name, int age, string cllass );
    void printAtributes ( void ) const;

  protected:
    string                    nameOfStudent;
    string                    whichClass;
    int                       ageOfStudent;
};
//========================================================================
class Group
{
  public:
    Group ( void );
    bool addStudent ( const Student & X );
    void printAll( void ) const;

  protected:
    vector<Student> vectorOfStudents;
};
//========================================================================
class All
{
  public:
    All ( void );
    bool addToAll ( const Group & T );
    void printAllofThem ( void ) const;

  protected:
    vector<Group> vectorOfAll;
};
//========================================================================
All::All ( void )
{
}
//------------------------------------------------------------------------
bool All::addToAll ( const Group & T )
{
  vectorOfAll . push_back ( T );
  return true;
}
//------------------------------------------------------------------------
void All::printAllofThem ( void ) const  // Function which iterate thought group objects
{
  cout << "-------------------" << endl;
  for ( const auto & allofthem : vectorOfAll )
  {
    allofthem . printAll  (  );
    cout << endl;
  }
}
//------------------------------------------------------------------------
Student::Student ( string name, int age, string cllass )
             :nameOfStudent( name ), ageOfStudent( age ), whichClass( cllass )
{
}
//--------------------------------------------------------------------
void Student::printAtributes ( void ) const
{
  cout << "name: " << nameOfStudent << " | " << "age: " << ageOfStudent << " | " << whichClass << endl;
}
//============================================================================
Group::Group ( void )
{
}
//----------------------------------------------------------------------------
bool Group::addStudent ( const Student & X )
{
  vectorOfStudents . push_back ( X );
  return true;
}
//----------------------------------------------------------------------------
void Group::printAll ( void ) const
{
  cout << "-------------------" << endl;
  for ( const auto & student : vectorOfStudents )
  {
    student . printAtributes (  );
    cout << endl;
  }
}
//----------------------------------------------------------------------------
int main()
{

  All           all; // Representing all classes
  Group         A1;
  Group         B1;

  all . addToAll ( A1 );
  all . addToAll ( B1 );

  A1 . addStudent ( Student ( "Mark", 20, "A1" ) );
  A1 . addStudent ( Student ( "Alan", 20, "A1") );
  A1 . addStudent ( Student ( "Eric", 19, "A1" ) );

  B1 . addStudent ( Student ( "John", 19, "B1" ) );

  A1 . printAll  ( );
  B1 . printAll  ( );

  all . printAllofThem ( );

  return 0;    

}

When you added A1 and B1 to all , both of those groups were still empty - and you took copies of them. When you subsequently added students to A1 and B1 , those groups got new students, but the groups in all are entirely different groups - they remain unmodified.

Either

  • Add the students to the groups first , then add the groups to all .
  • Have the groups be shared_ptr<Group> instead, with All having a vector<shared_ptr<Group>> . This way, the ordering doesn't matter since there will only be two group objects that everybody is simply sharing ownership of.

Side-note. This is a truly excessive amount of spaces:

B1 . printAll  ( );

You don't need... any of them:

B1.printAll();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM