在动态分配的指针数组中扩展内存

时间:2017-02-02 20:18:16

标签: c++ arrays class object expand

我想在添加对象时扩展我的数组。但它不会起作用。我一直在搜索和搜索,但找不到问题...它不认为这对训练有素的眼睛来说是一项艰巨的任务,所以我想我可以问你们。我有两个课程,其中课程TimberTimberRegister相关联,但Timber无法看到TimberRegisterTimberRegister可以看到它。我认为它应该足以显示TimberRegister和主文件的cpp文件,因为我很确定其他文件是okey并且不应该影响错误,这似乎是toString不能写第三个对象,因为它没有内存。

#include "timberRegister.h"
#include <iostream>

int main() {

    TimberRegister oak("Oak");
    oak.addTimber("20x10", 4, 10.50);
    oak.addTimber("28x14", 4, 15.00);
    oak.addTimber("15x5", 2, 5);

    int nrOf = oak.getNrOfTimber();
    string* str = new string[nrOf];
    oak.getTimberAsString(str, nrOf);
    cout << oak.getTitle() << endl;
    for (int i = 0; i < nrOf; i++) {
    cout << str[i] << endl;
    }

    return 0;
}
#include "timberRegister.h"

void TimberRegister::expand() {
    this->capacity += 10;
    Timber* *tmp = new Timber*[capacity];

    for (int i = 0; i < this->nrOfTimber; i++) {
        tmp[i] = this->timber[i];
    }
    delete[] this->timber;
    this->timber = tmp;
    this->initiate(this->nrOfTimber);
 }

 void TimberRegister::initiate(int from) {
     for (int i = 0; i < this->capacity; i++) {
        this->timber[i] = nullptr;
     }
 }

 void TimberRegister::freeMemory() {
 for (int i = 0; i < this->nrOfTimber; i++) {
        delete this->timber[i];
 }
    delete[] this->timber;
 }

 int TimberRegister::find(string dimension, int meters, double price) {
    int place = -1;
    Timber tmp(dimension, meters, price);

for (int i = 0; i < this->nrOfTimber && place == -1; i++) {
    if (*this->timber[i] == tmp) {
        place = i;
    }
}
return place;
}

TimberRegister::TimberRegister(string title) {
this->title = title;
this->nrOfTimber = 0;
this->capacity = 2;
this->timber = new Timber*[this->capacity];
for (int i = 0; i < capacity; i++) {
    this->timber[i] = nullptr;
}
this->initiate(this->nrOfTimber);
}

TimberRegister::TimberRegister(const TimberRegister &origObj) {
if (this != &origObj) {
    this->title = origObj.title;
    this->nrOfTimber = origObj.capacity;
    this->timber = new Timber*[origObj.capacity];
    for (int i = 0; i < origObj.nrOfTimber; i++) {
        this->timber[i] = new Timber(*origObj.timber[i]);
    }
    this->initiate(origObj.getNrOfTimber());
}
}

TimberRegister::~TimberRegister() {
this->freeMemory();
}

TimberRegister TimberRegister::operator=(const TimberRegister & origObj) {
this->freeMemory();

this->title = origObj.title;
this->nrOfTimber = origObj.nrOfTimber;
this->capacity = origObj.capacity;
this->timber = new Timber*[origObj.capacity];
for (int i = 0; i < origObj.capacity; i++) {
    this->timber[i] = new Timber(*origObj.timber[i]);
}
this->initiate(origObj.getNrOfTimber());

return *this;
}

bool TimberRegister::addTimber(string dimension, int meters, double price) {
bool added = false;

if (this->existTimber(dimension, meters, price) == false) {
    if (this->nrOfTimber >= this->capacity) {
        this->expand();
    }
    this->timber[nrOfTimber++] = new Timber(dimension, meters, price);
    added = true;
}
return added;
}

bool TimberRegister::removeTimber(string dimension, int meters, double    price) {
bool removed = false;
int tmp = -1;
tmp = this->find(dimension, meters, price);

if (tmp != -1) {
    delete this->timber[tmp];
    this->timber[tmp] = this->timber[--this->nrOfTimber];
    removed = true;
}
return removed;
}

bool TimberRegister::existTimber(string dimension, int meters, double price) {
bool found = false;
Timber tmp(dimension, meters, price);

for (int i = 0; i < nrOfTimber && found == false; i++) {
    if (*this->timber[i] == tmp) {
        found = true;
    }
}
return found;
}

int TimberRegister::getNrOfTimber() const {
return this->nrOfTimber;
}

string TimberRegister::getTitle() const{
return this->title;
}

void TimberRegister::setTitle(string title) {
this->title = title;
}

void TimberRegister::clear() {
freeMemory();
this->timber = new Timber*[capacity];
}

void TimberRegister::getTimberAsString(string arr[], int nrOf) const {
for (int i = 0; i < nrOf; i++) {
    arr[i] = this->timber[i]->toString();
}
}

2 个答案:

答案 0 :(得分:3)

  

但它不起作用。我一直在搜索和搜索,但找不到问题。

我注意到一个问题:

void TimberRegister::initiate(int from) {
     for (int i = 0; i < this->capacity; i++) {
        this->timber[i] = nullptr;
     }
 }

在该函数中,您正在创建所有指针nullptr。你需要使用:

void TimberRegister::initiate(int from) {
     for (int i = from; i < this->capacity; i++) { // i = from, not i = 0
        this->timber[i] = nullptr;
     }
 }

答案 1 :(得分:2)

首先,就像评论指出的那样:

std::vector

可能是更好的解决方案。

其次,我建议统一缩进代码,以便于阅读。

最后,(如果你的表总是最终只有空指针)我认为这是问题所在:

 void TimberRegister::initiate(int from) {
 for (int i = 0; i < this->capacity; i++) {
    this->timber[i] = nullptr;

不应ifrom开始吗?即int i = from