0

In my example I created a class Person which has a member object: struct data. This object contains data about person. Each time a Person-Object is created, also the data-object shall be initialized.

Observation: When adding object initializer to code (1) at class constructor I get failure message:
incomplete type is not allowedC/C++(70)

class person {
public:
  struct data;
  person() { /* (1) */
    person::data myPersonData;
  }

private:
};

So here is how I practice it now:

  1. No struct object initialization myPersonData in class person constructor (class_person.hpp)
  2. Create person object in main.cpp
  3. Create myPersonData in main.cpp (I would like to save this initialization and put it to class contructor)

The whole example looks like this:

// 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;
    };

_

// 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;
}
1
  • struct data; just declares person::data; the struct is lacking a definition and is thus incomplete Commented Oct 22, 2020 at 9:32

1 Answer 1

1

You should put the definition of data inside the definition of person if you want a member of it. Something like this.

#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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.