矢量存储对象

时间:2016-03-04 14:04:19

标签: c++ class vector

我创建了3个班级。

  • 所有课程 - 存储所有群组
  • 小组课程 - 将学生的对象存储到矢量中。
  • 学生课 - 存储学生的信息。

我遇到了All类的问题 - 方法printAllof它应该遍历Group objets并调用printAll中的方法,它应该遍历所有Student对象并调用printAtributes,但输出如下所示:

all . printAllofThem ( );

输出:

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

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

预期产出:

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

name: Alan | age: 20 | A1

name: Eric | age: 19 | A1

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

只有当我在main函数中调用它时才能给出正确的输出:

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

代码:

#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;    

}

1 个答案:

答案 0 :(得分:1)

当您将A1B1添加到all时,这两个群组仍然是空的 - 您可以复制它们。当您随后将学生添加到A1B1时,这些小组会有新学生,但all中的小组是完全不同的群组 - 他们保持不变。

无论

  • 将学生添加到然后将组添加到all
  • 让这些群组成为shared_ptr<Group>All拥有vector<shared_ptr<Group>>。这样,排序并不重要,因为只有两个组对象,每个人都只是共享所有权。

边注。这是一个真正过多的空间:

B1 . printAll  ( );

你不需要......其中任何一个:

B1.printAll();