我已经创建了三个类。
对于所有类方法printAllofThem,我有一个问题--它应该通过printAll中的Group和call方法进行迭代,应该通过所有的Student对象进行迭代,并调用printAtributes,但是输出如下所示:
all . printAllofThem ( );输出:
-------------------
-------------------
-------------------预期产出:
-------------------
name: Mark | age: 20 | A1
name: Alan | age: 20 | A1
name: Eric | age: 19 | A1
-------------------
name: John | age: 19 | B1只有当我在主函数中这样调用它时,它才能给出正确的输出:
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;
}发布于 2016-03-04 14:11:47
当您将A1和B1添加到all中时,这两个组仍然是空的--而且您还复制了它们。当您随后将学生添加到A1和B1中时,这些组获得了新的学生,但是all中的组是完全不同的组--它们保持不变。
任一
all中。shared_ptr<Group>,All有一个vector<shared_ptr<Group>>。这样,排序就不重要了,因为只有两个组对象,每个人都只是共享它们的所有权。旁白。这是一个非常大的空间:
B1 . printAll ( );你不需要。他们中的任何一个:
B1.printAll();https://stackoverflow.com/questions/35798043
复制相似问题