我創建了3個類。矢量存儲對象
- 所有類 - 存儲所有組
- Group類 - 儲存學生的對象爲載體。
- Studen類 - 存儲有關學生的信息。
我有所有類別的問題 - 方法printAllofThem應該通過組OBJETS迭代,並調用printAll方法應雖然遍歷所有學生對象,並調用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;
}
這聽起來像你可能需要學習如何使用調試器來逐步你的代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。詳細閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver
您正在致電,例如'all.addToAll(A1)'*之前*使用'A1.addStudent(...)'修改它。這些更改不會自動傳播到先前已添加到「all」對象的'A1'副本。 – nrussell
'addToAll'做了'vector.push_back',這反過來又製作了你所推的東西的副本。既然你用空組調用'addToAll','All'對象是空的。嘗試在'addStudent'之後執行'addToAll'' – Nacho