P
P
PUTINVODKAGTA2020-12-11 21:17:38
C++ / C#
PUTINVODKAGTA, 2020-12-11 21:17:38

Increasing the size of an array of objects?

The code
#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];
  
};


Good evening! Tell me what's the problem.

According to the assignment, you need to create a class with 2 fields Name and date. The class must also contain an array of class objects.
There are 2 questions:
1) Is the array declared correctly?
Class_ListPerson* list_person = new Class_ListPerson[size]; 
//size = 0, т.к. при других значениях ничего не происходит

2) When adding an array element, after declaring a new array of size + 1, the program breaks in the for loop, where the elements are copied. The following error appears in the debugger:

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

3 answer(s)
R
rPman, 2020-12-11
@PUTINVODKAGTA

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).

V
Vladimir A, 2020-12-12
@hauptling

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

G
galaxy, 2020-12-11
@galaxy

When adding an array element, after declaring a new array of size +1

New array of what-what size?
Class_ListPerson* list_person_temp = new Class_ListPerson[size++];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question