无法在C ++中删除动态数组的内容

时间:2019-02-07 05:29:33

标签: c++ arrays object

我一直在反对这个问题。在我的类的解构函数中,我有一个for循环,该循环应该遍历对象数组并删除它们。当我尝试时,出现读取访问冲突。所附的代码应该从两个文档中读取信息,并使用它来创建Country对象。

#include "pch.h"
#include "CountryCatalogue.h"
#include "Country.h"
#include <iterator>
#include <map>
//imports for reading the files
#include <iostream>
#include <fstream>

CountryCatalogue::CountryCatalogue()
{
    _maxSize = 10;
    _curSize = 0;
    _catalogue = new Country*[_maxSize];
}

CountryCatalogue::CountryCatalogue(std::string continentFileName, std::string countryFileName)
{

//block that opens the files and checks to make sure they can be read
//open up the files
std::ifstream inFile1;
std::ifstream inFile2;

//opening both text files and ensuring that the file is readable to the program
inFile1.open(continentFileName);
if (!inFile1) {
    std::cout << "Unable to open file";
    exit(1); // terminate with error
}

inFile2.open(countryFileName);
if (!inFile2) {
    std::cout << "Unable to open file";
    exit(1); // terminate with error
}

// read the continet file
// while there is still stuff to read in the file
std::string str;

while (!inFile1.eof())
{
    std::string Country, Cont;

    //reading lines from file and assigning to variables
    std::getline(inFile1, Country);
    std::getline(inFile1, Cont);

    //mapping to variables read from file
    _countryContinent.insert(std::pair<std::string, std::string>(Country, Cont));
    _curSize++;
}

//closing file after use
inFile1.close();

//creating array
_catalogue = new Country*[_curSize+2];

//resetting size to zero for later itteration
_curSize = 0;

// read the country file
// while there is still stuff to read in the file
while (!inFile2.eof())
{
    std::string name, POP, AREA;
    int pop;
    double area = 0.0;

    std::getline(inFile2, name);
    std::getline(inFile2, POP);
    std::getline(inFile2, AREA);

    if (!POP.empty() && POP[POP.length() - 1] == '\n') {
        POP.erase(POP.length() - 1);
    }
    if (!AREA.empty() && AREA[AREA.length() - 1] == '\n') {
        AREA.erase(AREA.length() - 1);
    }

    pop = std::stoi(POP);
    area = std::stod(AREA);

    //creating iterator to search through mapped values
    std::map<std::string, std::string>::iterator it;
    it = _countryContinent.find(name);

    //creating empty string variable to store continent
    std::string cont;

    //using value found by iterator to make continent string
    //ensuring value isn't the end valueof the map
    if (it != _countryContinent.end()){
        cont = it->second;
    }

    //std::cout << name << pop << area << cont << std::endl;

    // add the country to the catalogue
    addCountry(name, pop, area, cont);

    }
}

CountryCatalogue::~CountryCatalogue() {

    /*for (int i = 0; i < _curSize; i++){
        delete _catalogue[i];
        std::cout << "deleted" << i << std::endl;
    }*/
    delete[] _catalogue;
}

void CountryCatalogue::addCountry(std::string name, int pop, double area,     std::string cont) {

    //std::cout << name << pop << area << cont << std::endl;
    //std::cout << _curSize << std::endl;

    Country* toAdd = new Country(name, pop, area, cont);
    if (_curSize == _maxSize) {
        expandCapacity();
    }

    //adding country object to array
    _catalogue[_curSize] = toAdd;

    //adding to _curSize for next iteration
    _curSize++;
}


void CountryCatalogue::printCountryCatalogue() {

    std::string s;


    /*for (int i = 0; i < _curSize; i++) {
        s += _catalogue[i]->to_string() + "\n";
    }*/ 

    std::cout << _curSize << std::endl;
}

void CountryCatalogue::expandCapacity() {

    //doubling array size
    _maxSize = _maxSize * 2;

    //creating pointer to new array of new size
    Country** newCatalogue = new Country*[_maxSize];

    //copying old array into new
    for (int i = 0; i < _curSize; i++) {
        newCatalogue[i] = _catalogue[i];
    }

    //deleting old array
    delete[] _catalogue;

    //making _catalogue point to newCatalogue
    _catalogue = newCatalogue;
}

更新: 我的代码应该做的是从文本文件中获取信息,并使用该数据创建对象。我需要使用数组而不是向量。代码运行良好,我可以创建国家对象。问题是我无法将创建的对象添加到_catalogue数组,因为之后无法删除它。当我尝试遍历数组时,收到一条消息,指出检测到堆损坏。

2 个答案:

答案 0 :(得分:0)

您将_catalogue创建为动态数组。

要使用new TYPE[SIZE]释放分配给元素数组的内存,语法为:

delete[] _catalogue; 

需要回路才能删除分配给Matrix元素的内存。例如

int matrix = new int[rows][cols];
for (int i = 0; i < rows; ++i)
    delete [] matrix[i];   

该数组被逐行删除。

答案 1 :(得分:0)

您的问题是由于此行

.nav-link-type-three {
padding-right: 1.5rem !important;
padding-left: 1.5rem !important;
color: white !important;
text-decoration: none;
position: relative;
display: flex;
align-items: center;
justify-content: flex-end;
margin-right: 1rem;
}
.nav-link-type-three:hover{
text-decoration: none;
}
.nav-link-type-three::after,  li.show .nav-link-type-three::after{
position: absolute;   
background:url("your image path goes here...") no-repeat;
width:16px;
height:16px;
content: "";
 right:0
}
li.show .nav-link-type-three::after{
  transform:rotate(180deg)
}

在第二个构造函数中。您忘记更新_catalogue = new Country*[_curSize+2]; ,因此_maxSize与实际分配的内存量之间不匹配。

尝试:

_maxSize