【发布时间】:2015-12-01 10:21:28
【问题描述】:
这是我在这里的第一个问题,所以请善待:)
我正在编写一个程序来接收姓名、姓氏和数字/电子邮件地址,并且必须使用继承从基类 Person 创建一个类 person_with_telephone。
我已经尝试过,甚至接近了,但总是会弹出一些错误,因为我是 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()
{
}
};
忽略空的成员函数,它们稍后出现。关于我为什么会收到错误的任何想法:
main.cc: In constructor ‘Person_with_telephone::Person_with_telephone(const string&)’:
main.cc:59:73: error: no matching function for call to ‘Person::Person()’
Person_with_telephone(const string& telephone) : m_telephone(telephone)
^
main.cc:59:73: note: candidates are:
main.cc:13:3: note: Person::Person(const string&, const string&, std::string)
Person(const string& firstName, const string& lastName, const string telephone) :
^
main.cc:13:3: note: candidate expects 3 arguments, 0 provided
main.cc:6:7: note: Person::Person(const Person&)
class Person
^
main.cc:6:7: note: candidate expects 1 argument, 0 provided
<builtin>: recipe for target 'main' failed
make: *** [main] Error 1
Compilation exited abnormally with code 2 at Tue Dec 1 10:17:39
感谢您的帮助! :)
【问题讨论】:
-
您的
Person已经有电话了。您的Person_with_telephone添加了另一部电话。也许您应该将其命名为Person_with_two_telephones?另外,class Person_with_telephone的构造函数应该命名为Person_with_telephone,而不是Person。
标签: c++ inheritance compiler-errors