Populating a vector of objects within a class

2019-07-23 20:12发布

问题:

and I've hit my first wall on my coding project/assignment.

I'm to implement functionality into code that's been done to some stage, and I cannot alter the given code so I have to work around the given structure.

The code, in a nutshell, reads family relations from a text file and populates database with the family relation data and later on allows user to print out information he wants to access.

What I'm having trouble with is understanding how I can and how I have to utilize a struct given to me in the assignment. The struct is

struct Person
{
    std::string id_ = NO_ID;
    int height_ = NO_HEIGHT;
    std::vector<Person*> parents_{nullptr, nullptr};
    std::vector<Person*> children_;
};

and I'm using it at least in the initialization phase of the data structure.

I start by calling the process in main.cpp with

database->addRelation(it->child_, it->parents_, std::cout);

In the naming/height adding phase I'd simply do it with

MyPerson.id_ = id;
MyPerson.height_ = height;

where MyPerson is defined by Person MyPerson;

but as far as I can tell, I have to somehow access the object pointers to be able to populate the vectors for when I want to add children/parents to the person.

The class functions that are called when initializing person's name, height and family relations are these two:

void Familytree::addNewPerson(const string &id, const int &height, ostream &output)
{
    MyPerson.id_ = id;
    MyPerson.height_ = height;
}

void Familytree::addRelation(const string &child,
                             const std::vector<string>
                             &parents, ostream &output)
{

}

The addRelation fuction is what I'm having a hard time getting to work. Simply appending the strings to it won't work since it expects Person* -objects, which are, as far as I can tell, just pointers to the other Persons, but I'm not sure how I can access them.

Also, let me know if anything here is excessive or if I'm missing anything crucial, I'll edit it to the best of my ability

Editing with additional information:

The only things I've added myself that can be seen here is Person MyPerson;

and the contents of the class function addNewPerson. The other snippets I can not change in any shape or form.

Edit#2

Current progress, debatable whether I'm closer or further from the goal

My persons map is using Personmap = std::map<std::string, Person >;

and I'm using it in addNewPerson with

persons_[id] = id;
persons_[id] = height;

, but I'm still randomly trying different things to try and make it work for the next phase where I need to somehow add the objects to the vectors.

The biggest problem I have is the fact that I do now know how to play around the difference of *Person and Person