【问题标题】:How do I use have a constructor with another class object in c++?如何在 C++ 中使用带有另一个类对象的构造函数?
【发布时间】:2021-08-30 09:12:08
【问题描述】:

我有一个简单的程序,其中有两个类,分别是 HatPerson。每个Person 都有一个string name、一个int idNum 和一个帽子对象。每顶帽子只有一个字符串hatType 和一个字符hatSize。在主要方法中,我想简单地声明 2 个人并使用显示方法来显示信息。这是我当前的代码,请放轻松我还是 C++ 中 OOP 的新手。

人员类

class Person
{
  private:
    string name;
    unsigned int idNum;
    Hat myHat;

  public:
    Person(string, unsigned int, Hat);
    void display();
};


Person::Person(string personName, unsigned int personID)
{
    name = personName;
    idNum = personID;
    myHat = hat;
}

void Person::display()
{
        cout << "Given name : " << name << endl;
        cout << "Id. number : " << idNum << endl;
        hat.display();
}

帽子类

class Hat
{
  private:
    string hatType;
    char hatSize; // S, M, L

  public:
    Hat(string,char);
    void display();
};

Hat::Hat(string _type, char _size){
    hatType = _type;
    hatSize = _size;
}

void Hat::display()
{
    cout << "Hat type   : " << hatType << endl;
    cout << "Hat size   : " << hatSize << endl;
}

主要

int main()
{

    Person personA("Alice",12321, Hat("Trilbee",'M'));

    Person personB("Bob",2324, Hat("Ferret",'S'));

    personA.display();
    personB.display();

    return 0;
}

【问题讨论】:

  • 在 Person 构造函数中缺少帽子:Person::Person(string personName, unsigned int personID)
  • 将帽子添加到构造函数:Person::Person(string personName, unsigned int personID, const Hat& hat)
  • 当我添加 Hat hat 时出现此错误:没有匹配函数调用 'Hat::Hat()' @Cid
  • 另一件事:对于这些简单的初始化,最好使用成员初始化列表en.cppreference.com/w/cpp/language/constructor

标签: c++ oop constructor


【解决方案1】:

你的 Person 类在这里:

 Person::Person(string personName, unsigned int personID) 
 {
  . ...

没有完全实现,只是添加了hat参数...

Person::Person(string personName, unsigned int personID, Hat hat)
{....

【讨论】:

  • 这将创建一个不必要的 Hat 对象副本。所以最好使用 const Hat&,因为构造函数不能改变 hat 的内容,所以 const 存在。
  • 是的,我刚刚发现了这个。即使我确实添加了一条错误消息,提示“没有匹配函数调用'Hat::Hat()'。任何想法为什么?
  • @DamonO'Neil - 因为您的构造函数没有进行初始化,它们正在执行赋值。见stackoverflow.com/q/926752/817643
  • @StoryTeller-UnslanderMonica 在这种情况下如何初始化它?
  • @DamonO'Neil - 您阅读了我提供的链接,其中包含许多示例。
【解决方案2】:

以下是完整的更正(工作)版本。我添加了一些 cmets 来显示我所做的更改。

#include <iostream>
#include <string>
using namespace std;
class Hat
{
  private:
    string hatType;
    char hatSize; // S, M, L

  public:
    Hat(string,char);
    //Added this default constructor since it won't be synthesized automatically
    Hat()
    {
        std::cout<<"Default constructor"<<std::endl;
    }
    void display();
};

Hat::Hat(string _type, char _size){
    hatType = _type;
    hatSize = _size;
}

void Hat::display()
{
    cout << "Hat type   : " << hatType << endl;
    cout << "Hat size   : " << hatSize << endl;
}
class Person
{
  private:
    string name;
    unsigned int idNum;
    Hat myHat;

  public:
    Person(string, unsigned int, Hat);
    void display();
};

//added the 3rd parameter of type Hat
Person::Person(string personName, unsigned int personID, Hat hat)
{
    name = personName;
    idNum = personID;
    myHat = hat;
}

void Person::display()
{
        cout << "Given name : " << name << endl;
        cout << "Id. number : " << idNum << endl;
        myHat.display();//changed hat.display() to myHat.display();
}

int main()
{
    Person personA("Alice",12321, Hat("Trilbee",'M'));

    Person personB("Bob",2324, Hat("Ferret",'S'));

    personA.display();
    personB.display();

    return 0;
}

【讨论】:

  • 抱歉,这么久才回复您。非常感谢您提供这个,我现在完全理解了!
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
相关资源
最近更新 更多