首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >矢量存储对象

矢量存储对象
EN

Stack Overflow用户
提问于 2016-03-04 14:04:19
回答 1查看 100关注 0票数 0

我已经创建了三个类。

  • 所有类-存储所有组
  • 将学生的类存储对象分组到向量中。
  • 学习课堂存储学生的信息。

对于所有类方法printAllofThem,我有一个问题--它应该通过printAll中的Group和call方法进行迭代,应该通过所有的Student对象进行迭代,并调用printAtributes,但是输出如下所示:

代码语言:javascript
复制
all . printAllofThem ( );

输出:

代码语言:javascript
复制
-------------------
-------------------

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

预期产出:

代码语言:javascript
复制
-------------------
name: Mark | age: 20 | A1

name: Alan | age: 20 | A1

name: Eric | age: 19 | A1

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

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

代码语言:javascript
复制
A1 . printAll  ( );
B1 . printAll  ( );

代码:

代码语言:javascript
复制
#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;    

}
EN

回答 1

Stack Overflow用户

发布于 2016-03-04 14:11:47

当您将A1B1添加到all中时,这两个组仍然是空的--而且您还复制了它们。当您随后将学生添加到A1B1中时,这些组获得了新的学生,但是all中的组是完全不同的组--它们保持不变。

任一

  • 先将学生添加到组中,然后将组添加到all中。
  • 将组改为shared_ptr<Group>All有一个vector<shared_ptr<Group>>。这样,排序就不重要了,因为只有两个组对象,每个人都只是共享它们的所有权。

旁白。这是一个非常大的空间:

代码语言:javascript
复制
B1 . printAll  ( );

你不需要。他们中的任何一个:

代码语言:javascript
复制
B1.printAll();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35798043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档