2017-04-02 69 views
0

我有一個小問題..我想做汽車註冊,我想擁有所有者(指向一個Person對象的指針),但是當我想帶所有者獲取函數我不能那樣做......這對我來說是神祕......也許我有一點點失誤的地方,我無法找到它。請幫助:)指針tho另一個對象

class Person 
{ 
    friend class Cars; 
public: 
    Person(){} 
    Person(uint8_t age, string name) :m_age(age), m_name(name) 
    { 
     m_idGener = s_idGenerator++; 
    } 

    int getId() 
    { 
     return m_idGener; 
    } 
    uint8_t getAge() 
    { 
     return m_age; 
    } 
    string getName() 
    { 
    return m_name; 
    } 
private: 
    string m_name; 
    uint8_t m_age; 
    static int s_idGenerator; 
    int m_idGener; 

}; 
class Cars:public Person 
{ 

public: 
    Person *m_owner; // this is my pointer which i want to point to cars  Object 
    it will stay public for the test after the test i will move it in the  private section 
    Cars() 
    { 
    } 
    // getters and setters 

    Cars setOwner(Cars &object, Person &owner) // with this function i set the owner 
    { 
     object.m_owner = &owner; 
    } 
    Cars getOwner(Cars &object) /// here is my problem i can't take the owner 
    { 
     return *(object.m_owner); // but i can't take the owner 
    } 

    uint16_t getHorsePower() 
    { 
     return horsePower; 
    } 
    string getRegNumber() 
    { 
     return registrationNumber; 
    } 
    private: 
    string m_manufacturerName; 
    string modelName; 
    uint16_t horsePower; 
    string registrationNumber; 

}; 

int Person::s_idGenerator = 0; 

回答

1

你犯了更多的錯誤。至少應該糾正以下幾點:

Cars setOwner(Cars &object, ..... 

這個方法doest沒有返回值,所以應該是void。汽車對象應該是調用方法的對象,而不是參數。

Cars getOwner(Cars &object) /// here is my problem i can't take the owner 
{ 
    return *(object.m_owner); // but i can't take the owner 
} 

在這種方法我將返回類型更改爲Person *,然後簡單地返回m_owner。參數是不需要的。

修改過的工作例如:

#include <string> 
#include <iostream> 
using namespace std; 
class Person 
{ 
    friend class Cars; 
public: 
    Person(){} 
    Person(uint8_t age, string name) :m_age(age), m_name(name) 
    { 
     m_idGener = s_idGenerator++; 
    } 

    int getId() 
    { 
     return m_idGener; 
    } 
    uint8_t getAge() 
    { 
     return m_age; 
    } 
    string getName() 
    { 
    return m_name; 
    } 
private: 
    string m_name; 
    uint8_t m_age; 
    static int s_idGenerator; 
    int m_idGener; 

}; 
class Cars:public Person 
{ 

public: 
    Person *m_owner; // this is my pointer which i want to point to cars  Object 
         // it will stay public for the test after the test i will move it in the  private section 
    Cars() 
    { 
    } 
    // getters and setters 

    void setOwner(Person &owner) // with this function i set the owner 
    { 
     m_owner = &owner; 
    } 
    Person *getOwner() /// here is my problem i can't take the owner 
    { 
     return (m_owner); // but i can't take the owner 
    } 

    uint16_t getHorsePower() 
    { 
     return horsePower; 
    } 
    string getRegNumber() 
    { 
     return registrationNumber; 
    } 
    private: 
    string m_manufacturerName; 
    string modelName; 
    uint16_t horsePower; 
    string registrationNumber; 

}; 

int Person::s_idGenerator = 0; 

int main() { 
    Person p(5,"Bill"); 
    Cars c; 

    c.setOwner(p); 
    cout << c.getOwner()->getName(); 
} 
+0

非常感謝你我是初學者,現在我正在學習代碼:) –

+0

歡迎您。如果它對你有幫助,你可以考慮upvote。 – quantummind

0

這裏:

Cars setOwner(Cars &object, Person &owner) // with this function i set the owner 
{ 
object.m_owner = &owner; 
} 

你聲明超值類型爲「汽車」,但你不會返回任何東西。

在這裏:

Cars getOwner(Cars &object) /// here is my problem i can't take the owner 
{ 
return *(object.m_owner); // but i can't take the owner 
} 

你想回到「人」對象,但你又聲明瞭價值類型爲「汽車總動員」。

所以,你可以改變這種功能像:

第一:

void setOwner(Cars &object, Person &owner); 

二:

Person getOwner(Cars &object); 
相關問題