【问题标题】:Code won't inherit and I have no idea why代码不会继承,我不知道为什么
【发布时间】: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


【解决方案1】:

一个Person_with_telephone 是一个 Person。因此Person_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_with_telephonePerson 都有一个 m_telephone 成员。那会给你带来无尽的痛苦和困惑。如果他们都应该有这样的成员,请给他们不同的名字。

【讨论】:

    【解决方案2】:

    看起来你有错误的 Person_with_telephone 构造函数。它应该有名称 Person_with_telephone。它还应该调用 Person 的构造函数,因为它没有默认构造函数。这也很奇怪,因为您的 Person 类有 m_telephone 字段。试试这个构造函数:

    Person_with_telephone(const string& firstName, const string& lastName, const string telephone) : Person(firstName,lastName, telephone), m_telephone(telephone)
      {};
    

    也许您需要从 Person 中删除 m_telephone。

    【讨论】:

      【解决方案3】:

      在派生类中,你必须显式调用 Base 构造函数,如果你不调用它,它被称为隐式,在这种情况下,它将被称为隐式,当这种情况发生时,将调用来自基的默认构造函数。在你的基础中,你已经声明了带有 3 个参数的构造函数,并且没有创建默认值,所以错误在说什么。如果您不想显式调用它,则必须在基础中创建默认值,或者调用您在派生初始化列表中定义的值。

      【讨论】:

        猜你喜欢
        • 2017-07-08
        • 1970-01-01
        • 2020-09-19
        • 2014-01-17
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 2021-02-18
        • 1970-01-01
        相关资源
        最近更新 更多