[英]Vector storing objects
I have created 3 classes. 我创建了3个课程。
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它应该遍历Group objets并在printAll中调用方法,该方法应该遍历所有Student对象并调用printAtributes,但是输出看起来像这样:
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: 仅当我在main函数中这样调用它时,它才给我正确的输出:
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. 当您将
A1
和B1
添加到all
,这两个组仍然为空-并已将它们复制。 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. 当您随后将学生添加到
A1
和B1
,这些组获得了新学生,但是all
组都是完全不同的组-它们保持不变。
Either 要么
all
. all
组添加到中。 shared_ptr<Group>
instead, with All
having a vector<shared_ptr<Group>>
. shared_ptr<Group>
,而All
都有一个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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.