Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何通过类构造函数创建成员对象?_C++_Class_Constructor - Fatal编程技术网

C++ 如何通过类构造函数创建成员对象?

C++ 如何通过类构造函数创建成员对象?,c++,class,constructor,C++,Class,Constructor,在我的示例中,我创建了一个类Person,它有一个成员对象:struct data。此对象包含有关个人的数据。每次创建Person对象时,也应初始化数据对象 观察:在类构造函数的代码(1)中添加对象初始值设定项时,我收到失败消息: 不允许使用不完整的类型C/C++(70) 下面是我现在的练习方式: 类person构造函数(class_person.hpp)中没有结构对象初始化myPersonData 在main.cpp中创建person对象 在main.cpp中创建myPersonData(我想

在我的示例中,我创建了一个类Person,它有一个成员对象:struct data。此对象包含有关个人的数据。每次创建Person对象时,也应初始化数据对象

观察:在类构造函数的代码(1)中添加对象初始值设定项时,我收到失败消息:
不允许使用不完整的类型C/C++(70)

下面是我现在的练习方式:

  • 类person构造函数(class_person.hpp)中没有结构对象初始化myPersonData
  • 在main.cpp中创建person对象
  • 在main.cpp中创建myPersonData(我想保存这个 初始化并将其放入类构造函数)
  • 整个示例如下所示:

    // class_person.hpp
        
        #include <iostream>
        #include <string>
        
        class person {
        public:
          struct data;
        
        private:
        };
        
        struct person::data {
          std::string name = "John";
          int age = 42;
          int weight = 75;
        };
    
    //class\u person.hpp
    #包括
    #包括
    班主任{
    公众:
    结构数据;
    私人:
    };
    struct person::data{
    std::string name=“John”;
    年龄=42岁;
    整数重量=75;
    };
    
    _

    //main.cpp
    #包括
    #包括“人员等级hpp”
    void outputPersonData(person::data myPerson){
    
    std::cout如果你想成为
    data
    的一员,你应该把
    data
    的定义放在
    person
    的定义里面。类似这样

    #include <string>
    #include <iostream>
    
    class person {
    public:
        struct data {
            std::string name = "John";
            int age = 42;
            int weight = 75;
        };
        // This is just the definition the the class. We need the class
        // to have a definition if we want to use a value of it in person
    
        person() = default;
        // Default constructors, give us a default constructed personData
        // that uses the default values from the definition
    
        person(data d) : personData(std::move(d)) {}
        // Constructor that takes a personData object and uses it to
        // initialize our member. std::move is to avoid uneccesary copying
    
        void outputPersonData() const {
            std::cout << personData.name << "\n";
            std::cout << personData.age << "years\n";
            std::cout << personData.weight << "kg\n";
        }
    
        data personData;
        // This is the actual data member, now person contains
        // a member named personData of type person::data
    };
    
    int main() {
        person john;
        person mike({"Mike", 47, 82});
    
        john.outputPersonData();
        mike.outputPersonData();
    }
    
    #包括
    #包括
    班主任{
    公众:
    结构数据{
    std::string name=“John”;
    年龄=42岁;
    整数重量=75;
    };
    //这只是这个类的定义,我们需要这个类
    //如果我们想亲自使用它的值,就要有一个定义
    person()=默认值;
    //默认构造函数,给我们一个默认构造的personData
    //使用定义中的默认值的
    person(数据d):personData(std::move(d)){}
    //构造函数,它接受personData对象并使用它来
    //初始化我们的成员。std::move是为了避免复制不成功
    void outputPersonData()常量{
    
    std::cout
    struct data;
    只是声明
    person::data
    ;该结构缺少定义,因此不完整
    // main.cpp
    
    #include <iostream>
    
    #include "class_person.hpp"
    
    void outputPersonData(person::data myPerson) {
      std::cout << myPerson.name << "\n";
      std::cout << myPerson.age << "years\n";
      std::cout << myPerson.weight << "kg\n";
    };
    
    int main() {
      person John;
      person::data myPersonData;
      outputPersonData(myPersonData);
      getchar();
      return 0;
    }
    
    #include <string>
    #include <iostream>
    
    class person {
    public:
        struct data {
            std::string name = "John";
            int age = 42;
            int weight = 75;
        };
        // This is just the definition the the class. We need the class
        // to have a definition if we want to use a value of it in person
    
        person() = default;
        // Default constructors, give us a default constructed personData
        // that uses the default values from the definition
    
        person(data d) : personData(std::move(d)) {}
        // Constructor that takes a personData object and uses it to
        // initialize our member. std::move is to avoid uneccesary copying
    
        void outputPersonData() const {
            std::cout << personData.name << "\n";
            std::cout << personData.age << "years\n";
            std::cout << personData.weight << "kg\n";
        }
    
        data personData;
        // This is the actual data member, now person contains
        // a member named personData of type person::data
    };
    
    int main() {
        person john;
        person mike({"Mike", 47, 82});
    
        john.outputPersonData();
        mike.outputPersonData();
    }