2016-08-16 89 views
-1

我的任務是在類中創建一個對象,初始化它並輸出(使用指向類的指針)。此代碼完美編譯,但輸出不顯示。我會很感激任何幫助,提前謝謝!對象指針不起作用

#include <iostream> 
#include <string> 

using namespace std; 

class family 
{ 
public: 
    void setWife(string w) 
    {w = wife;}; 
    string getWife() 
    {return wife;}; 
    void setHusband(string h) 
    {husband = h;}; 
    string getHusband() 
    {return husband;}; 
    void setSon(string s) 
    {s = son;}; 
    string getSon() 
    {return son;}; 
    void setDaughter1(string d1) 
    {d1 = daughter1;}; 
    string getDaughter1() 
    {return daughter1;}; 
    void setDaughter2(string d2) 
    {daughter2 = d2;}; 
    string getDaughter2() 
    {return daughter2;}; 
    double* getPointer() 
    {return &pointer;}; 
    void initialize() 
    { 
     setWife("Shirley Collin"); 
     setHusband("Donald Collin"); 
     setSon("Collin Collin"); 
     setDaughter1("Harriet Collin"); 
     setDaughter2("Hillary Collin"); 
    } 
    friend void output(family* Collin); 


private: 
    string wife; 
    string husband; 
    string son; 
    string daughter1; 
    string daughter2; 
    double pointer; 
}; 


void output(family* Collin) 
{cout << "Husband is " <<Collin->getHusband()<< endl; 
    cout << "wife is " << Collin ->getWife() << endl; 
    cout << "son is " << Collin->getSon() << endl; 
    cout << "daughter1 is " << Collin->getDaughter1() << endl; 
    cout << "daughter2 is " << Collin->getDaughter2()<< endl; 
}; 

int main() 
{family Collin; 
    Collin.initialize(); 
    family *pointer = new family; 
    output (pointer); 
    cin.ignore(); 

} 
+0

我猜你想'輸出(Collin)上;'。 – songyuanyao

+1

開始一個更簡單的類,讓指針的東西完美工作,然後添加所有其他的東西。 – Beta

回答

1
family Collin; 
Collin.initialize(); 

此構造family類的一個實例,並與在initialize()方法定義的值進行初始化。

family *pointer = new family; 
output (pointer); 

此構造family類的第二實例,以任何方式不進行初始化,並調用output()方法中,爲了顯示該family類的完全未初始化的第二個實例中的內容。

這就是爲什麼這個程序沒有產生有用的輸出。

你可能要替換這些四行:

family *pointer=new family; 
pointer->initialize(); 
output(pointer); 
+0

偉大的,1和最後一串輸出開始工作,我會嘗試修理剩菜,謝謝! – Muresha