Answer the question
In order to leave comments, you need to log in
Increasing the size of an array of objects?
#pragma once
#include <iostream>
#include <string>
#include "Class_Date.h"
class Class_ListPerson :public Date
{
public:
Class_ListPerson()
{
name = "";
}
~Class_ListPerson()
{
delete[]list_person;
}
void choose_fun(Class_ListPerson&one)
{
bool check = true;
int choice = 0;
while (check)
{
cout << "Выберите функцию:\n1.Добавить\n2.Показать" << endl;
cin >> choice;
switch (choice)
{
case 1:
add(one);
break;
case 2:
out(one);
break;
case 3:
cout << "Выход" << endl;
check = false;
break;
default:
cout << "Ошибка ввода!" << endl;
break;
}
}
}
void add(Class_ListPerson&one)
{
if (size==0)
{
list_person[size].init(name, date);
size++;
}
else
{
Class_ListPerson* list_person_temp = new Class_ListPerson[size++];
for (int i = 0; i < size; i++)
{
list_person_temp[i] = list_person[i];
}
list_person[size-1].init(name, date);
delete[]list_person;
list_person = list_person_temp;
}
}
void init(string&name,Date &date)
{
cout << "Введите имя: ";
cin >> name;
date.read();
}
void out(Class_ListPerson&one)
{
for (int i = 0; i < size; i++)
{
list_person[i].show_person(name, date);
}
}
void show_person(string& name, Date& date)
{
cout <<"Имя - "<< name << endl;
date.display();
}
private:
string name;
Date date;
int id;
int size=0;
Class_ListPerson* list_person = new Class_ListPerson[size];
};
Class_ListPerson* list_person = new Class_ListPerson[size];
//size = 0, т.к. при других значениях ничего не происходит
An exception was thrown at 0x00007FFBF98D163C (vcruntime140d.dll) in 33333.exe: 0xC0000005: Access violation while reading at address 0x0000015E54BD5FF8.
Answer the question
In order to leave comments, you need to log in
arrays are initialized differently
Class_ListPerson[size]={Class_ListPerson(...),Class_ListPerson(...),..};
if there is a constructor without arguments, then it is possible without everything on the right.
You cannot change the size of an array, since its size is not stored anywhere and is a constant, i.e. you will have to work not with the array itself, but with a reference to it and fill it manually through memory allocation (you can, of course, work with a reference to the first element like yours, but this is bad practics, references should be avoided by all means).
use containers like vector.
class Class_ListPerson{
public:
void addnewlist(){
Class_ListPerson object;
m_vector.push_back(object);
}
private:
std::vector<Class_ListPerson> m_vector;
char* m_name;
uint8_t m_data;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question