gpt4 book ai didi

c++ - 为什么当我调用 print() 函数时我的字符串不输出?

转载 作者:太空宇宙 更新时间:2023-11-04 16:25:06 25 4
gpt4 key购买 nike

我的类 extPersonType 是从其他 3 个类继承的。程序编译没有错误,但由于某种原因,字符串 relationphoneNumber 没有显示。我要求的所有其他信息都可以。我的问题在哪里?

class extPersonType: public personType, public dateType, public addressType
{
public:
extPersonType(string relation = "", string phoneNumber = "", string address = "", string city = "", string state = "", int zipCode = 55555, string first = "", string last = "",
int month = 1, int day = 1, int year = 0001)
: addressType(address, city, state, zipCode), personType(first, last), dateType (month, day, year)
{
}
void print() const;

private:
string relation;
string phoneNumber;
};

void extPersonType::print() const
{
cout << "Relationship: " << relation << endl;
cout << "Phone Number: " << phoneNumber << endl;
addressType::print();
personType::print();
dateType::printDate();
}



/*******
MAIN PROGRAM
*******/

int main()
{
extPersonType my_home("Friend", "555-4567", "5142 Wyatt Road", "North Pole", "AK", 99705, "Jesse", "Alford", 5, 24, 1988);
my_home .extPersonType::print();
return 0;
}

最佳答案

那是因为你没有在任何地方初始化它们

    extPersonType(string relation = "", string phoneNumber = "", string address = "", string city = "", string state = "", int zipCode = 55555, string first = "", string last = "", int month = 1, int day = 1, int year = 0001)
: relation (relation), phoneNumber (phoneNumber)// <<<<<<<<<<<< this is missing
addressType(address, city, state, zipCode), personType(first, last), dateType (month, day, year)
{
}

你不应该忘记在构造函数中分配/初始化你的变量

此外,这是推荐,但我真的不认为这里需要继承。你应该使用合成。

class extPersonType
{
private:
string relation;
string phoneNumber;

addressType address;
personType person_name;
dateType date; // birthday ?
}

关于c++ - 为什么当我调用 print() 函数时我的字符串不输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com