C++ 代码赢得';我不能继承,我也不知道为什么
这是我在这里的第一个问题,所以一定要友善:) 我正在编写一个程序来接收姓名、姓氏和数字/电子邮件地址,并且必须使用继承来从基类C++ 代码赢得';我不能继承,我也不知道为什么,c++,inheritance,compiler-errors,C++,Inheritance,Compiler Errors,这是我在这里的第一个问题,所以一定要友善:) 我正在编写一个程序来接收姓名、姓氏和数字/电子邮件地址,并且必须使用继承来从基类person创建一个带有电话的类person\u 我尝试过,甚至接近了,但是总是有那么几个错误出现,而且因为我是C++新手,我不知道它们是什么意思。 以下是相关代码: class Person { private: string m_FirstName, m_LastName, m_email, m_telephone; public: Person(con
person
创建一个带有电话的类person\u
<>我尝试过,甚至接近了,但是总是有那么几个错误出现,而且因为我是C++新手,我不知道它们是什么意思。
以下是相关代码:
class Person
{
private:
string m_FirstName, m_LastName, m_email, m_telephone;
public:
Person(const string& firstName, const string& lastName, const string telephone) :
m_FirstName(firstName), m_LastName(lastName), m_telephone(telephone)
{}
string get_name() const
{
return m_FirstName;
}
string get_surname() const
{
return m_LastName;
}
bool has_telephone_p()
{
if (m_telephone == "")
{
return false;
cout << "You have no phone number registered" << endl;
}
else
{
return true;
cout << "Your number is: " << m_telephone << endl;
}
}
string get_telephone() const
{
return m_telephone;
}
bool has_email_p()
{
}
};
class Person_with_telephone: public Person
{
private:
string m_telephone;
public:
Person(const string& telephone) : m_telephone(telephone)
{};
string set_telephone()
{
}
string get_telephone()
{
}
};
班级人员
{
私人:
字符串m_FirstName、m_LastName、m_email、m_telephone;
公众:
人员(常量字符串和名字、常量字符串和姓氏、常量字符串电话):
m_名字(名字),m_名字(名字),m_电话(电话)
{}
字符串get_name()常量
{
返回m_FirstName;
}
字符串get_name()常量
{
返回m_LastName;
}
布尔有电话
{
如果(m_电话==“”)
{
返回false;
coutAPerson\u with_telephone
是一个Person
。因此,一个Person\u with_telephone
的构造函数也在构造一个Person
。你没有它可以调用的默认构造函数,因此你必须向Person
构造函数提供参数
以下是语法:
class Int
{
public:
int j;
Int (int q) : j(q) { ; }
};
class IntAndString : public Int
{
public:
std::string t;
IntAndString(int q, std::string s) : Int(q), t(s) { ; }
};
另外,出于某种原因,Person\u with_phone
和Person
都有一个m\u phone
成员。这会给你带来无尽的痛苦和困惑。如果他们都应该有这样的成员,给他们起不同的名字。看起来你对Person\u with_phone的构造有误。应该有name Person_和电话。它还应该调用Person的构造函数,因为它没有默认构造函数。另外,这很奇怪,因为您的Person类有m_电话字段。请尝试此构造函数:
Person_with_telephone(const string& firstName, const string& lastName, const string telephone) : Person(firstName,lastName, telephone), m_telephone(telephone)
{};
可能您需要从Person中删除m_telephone。在派生类中,您必须显式调用Base constructor,如果您不这样做,它将被称为隐式,在这种情况下,它将被称为inplicit,当这种情况发生时,将调用Base中的默认构造函数。在您的Base中,您已声明构造函数使用3个参数,并且默认值不是由这就是错误所在。如果您不想明确地调用它,您必须在base中创建默认的一个,或者调用您在派生初始化列表中定义的一个。您的Person
已经有了一部电话。您的Person\u with\u phone
添加了另一部电话。也许您应该将其命名为Person\u with\u two\u tele电话
?此外,类Person\u with_telephone
的构造函数应命名为Person\u with_telephone
,而不是Person
。
Person_with_telephone(const string& firstName, const string& lastName, const string telephone) : Person(firstName,lastName, telephone), m_telephone(telephone)
{};