2016-03-04 115 views
0

我創建了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;  

} 
+1

這聽起來像你可能需要學習如何使用調試器來逐步你的代碼。使用一個好的調試器,您可以逐行執行您的程序,並查看它與您期望的偏離的位置。如果你打算做任何編程,這是一個重要的工具。詳細閱讀:** [如何調試小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver

+0

您正在致電,例如'all.addToAll(A1)'*之前*使用'A1.addStudent(...)'修改它。這些更改不會自動傳播到先前已添加到「all」對象的'A1'副本。 – nrussell

+0

'addToAll'做了'vector.push_back',這反過來又製作了你所推的東西的副本。既然你用空組調用'addToAll','All'對象是空的。嘗試在'addStudent'之後執行'addToAll'' – Nacho

回答

1

當您添加A1B1all,這兩個羣體的人仍然是空的 - 你拿了它們的副本。當您隨後將學生添加到A1B1時,這些組獲得了新學生,但all中的組完全不同 - 它們保持不變。

要麼

  • 學生添加到組第一然後添加組all
  • 讓這些組代替shared_ptr<Group>All代替vector<shared_ptr<Group>>。這樣,排序並不重要,因爲只有兩個組對象,每個人都只是共享所有權。

附註。這是一個空白真正過量:

B1 . printAll (); 

你不需要......任何人:

B1.printAll(); 
+0

是的,這是非常感謝你的問題。 – kvway

+0

關於這些空間,它看起來更多表格我coud它會導致任何問題? – kvway

+0

@kvway尋找表格是一個奇怪的代碼目標。但是,不,C++中的空白不重要。 – Barry