我有一点问题..我想做车注册,我想拥有(指向一个Person对象的指针),但是当我想带有获取功能的拥有者时,我无法做到这一点。这对我来说是个谜......也许我在某个地方犯了一个小错误,我找不到它..请帮忙:)
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;
答案 0 :(得分:1)
你犯了更多错误。至少应该纠正以下几点:
Cars setOwner(Cars &object, .....
此方法不会返回值,因此它应该是无效的。 car对象应该是调用方法的对象,而不是参数。
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();
}
答案 1 :(得分: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
}
您想要返回“Person”对象,但是您再次将值类型声明为“Cars”。
所以你可以改变这样的功能:
首先:
void setOwner(Cars &object, Person &owner);
第二
Person getOwner(Cars &object);