How do I add an object to an array in c++?

0

Im playing around with a tiny c++ project. Im trying to add an object of person (from my Person class) into an Array. But when i run my code it tells me that Person struct is NULL. However, it also sais that my person got a name and an adress, so it doesnt seem like its null?

I have a Person class:

.h

{
public:

    std::string name;
    std::string adress;

    Person() = default;
    Person(std::string name, std::string adress);

    void Print();

    ~Person();
};

.cpp

#include "Person.h"
#include <iostream>
#include <string>

Person::Person(std::string name, std::string adress)
{
    this->name = name;
    this->adress = adress;
}

void Person::Print()
{
    std::cout << "Namn: " << name << ". Adress: " << adress << std::endl;
}

Person::~Person()
{
}

I Have an PersonRegister class:

.h

#include "Person.h"

class PersonReg
{
public:

    int indexInt = 0;
    int maxSize = 50;
    Person *person;

    PersonReg() = default;
    PersonReg(int maxSize);

    void AddPerson(Person *person);
    void PrintPersons();
    void DeletePerson(Person *person);

    ~PersonReg();
};

.cpp

#include <string>
#include "Person.h"

PersonReg::PersonReg(int maxSize)
{
    this->maxSize = maxSize;

    person = new Person[maxSize];
}

void PersonReg::AddPerson(Person *person)
{
    person[indexInt++] = *person;

}

void PersonReg::PrintPersons()
{
    for (Person* personPtr = person; personPtr != person + maxSize; ++personPtr)
    {
        personPtr->Print();
    }
}

void PersonReg::DeletePerson(Person *person)
{
    if (person != nullptr)
    {
        for (Person *personPtr = person; personPtr != person + maxSize; ++personPtr)
        {
            personPtr->name = "NO NAME";
        }
    }
}


PersonReg::~PersonReg()
{
    delete[]person;
}

When I execute it, I get the Error: Exception thrown at 0x7AFC3729 (vcruntime140d.dll) in Labb3_c++.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

But in the output, I can see that person name and adress has values. Im reading to PersonReg from a text-file like this. This is my main:

#include "pch.h"
#include <iostream>
#include <fstream>
#include "Person.h"
#include "PersonReg.h"

bool ReadReg(PersonReg& reg, std::string fileName)
{
    std::string line;
    std::ifstream myfile(fileName);
    if (myfile.is_open())
    {
        while (getline(myfile, line))
        {
            while (line.length() == 0 && getline(myfile, line))
                ;
            std::string name(line);
            std::string adress;
            getline(myfile, adress);
            reg.AddPerson(&Person(name, adress));
        }
        myfile.close();
        return true;
    }
    else {
        std::cout << "Unable to open file";
        return false;
    }
}

void Test1(Person* person, PersonReg personReg)
{
    ReadReg(personReg, "PersonExempel.txt");
    personReg.PrintPersons(); std::cout << "\n\n";
    personReg.DeletePerson(person);
}

int main()
{
    Person *person = new Person();
    PersonReg personReg = PersonReg();
    Test1(person, personReg);

    delete person;
    return 0;
};

Im supposed to add a person in my personArray. Then print the person with a simple print function. And then delete the Person again.

EDIT: Sorry for long post but Im not sure where the problem is located (besides the memory). Im a totally beginner is C++ so it sure can look like a mess!

c++
arrays
asked on Stack Overflow Oct 8, 2019 by Emmaaaaaaa

2 Answers

0

Take a look at this code:

void PersonReg::AddPerson(Person *person)
{
    person[indexInt++] = *person;

}

You are using person on both left and right side. But what is person here? Is it the passed argument? Is it the member variable of the class?

It seems that you want the LHS to be the member variable while you want the RHS to be the passed argument. But how do you expect the compiler to know that?

You can use this->person to explicit refer to the member variable. However, I'll recommend a renaming of the member variable to something like personArray or a renaming of the argument like: void PersonReg::AddPerson(Person *pers).

Further:

PersonReg personReg = PersonReg();

Shouldn't this be

PersonReg personReg = PersonReg(42);
                                ^^
                             some number

so that you call the intended constructor, i.e. the constructor that calls new and reserve memory for the array.

All that said - I'll recommend that you use std::vector instead of an array allocated using new

answered on Stack Overflow Oct 8, 2019 by 4386427 • edited Oct 8, 2019 by 4386427
0

First of all the maxSize is not getting the value of 50. That's why there is not object Person. You should delete the PersonReg() = default; and keep the other constructor like this PersonReg(int maxSize=50);

As @PeterT mentioned you had a problem in this function. this-> should fix it:

void PersonReg::AddPerson(Person *person)
{
    this->person[indexInt++] = *person;

}

I corrected a few other things. PrintPersons could be this way and it works:

void PersonReg::PrintPersons()
{
    for(int i=0; i<indexInt; i++){
        person[i].Print();
    }
}

Also on the DeletePerson function I cannot understand what exactly you want to do. If you want to delete a spesific person you should change the function to this:

void PersonReg::DeletePerson(Person *person)
{
    if (person != nullptr)
    {
        for(int i=0; i<indexInt; i++){
            if(this->person[i].name==person->name&&this->person->adress==person->adress){
                this->person->name="NO NAME";
                this->person->adress="NO ADDRESS"; //or something like that.
            }
        }
    }
}

I tested all of these and they work. I hope I helped.

answered on Stack Overflow Oct 8, 2019 by FouLiNuX

User contributions licensed under CC BY-SA 3.0