I've been working on my spaceInvaders clone and I'm now trying to finish of the project with cleaning the memory leaks.
Currently I'm trying to delete an array of 11 aliensobjects created in a constructor, however whilst doing so the program breaks (crashes) at the destructor of AlienRow.
I've tried the following things:
Created nullpointers in constructor and deleted with this:
for (int i = 0; i < 11; i++)
{
if (alienRow[i] != nullptr)
{
delete alienRow[i];
}
delete *alienRow;
as well as:
delete [] alienRow;
Any pointers to why this issue occurs?
#include "AlienRow.h"
AlienRow::AlienRow(float x, float y, int type){
for (int i = 0; i < 11; i++)
{
alienRow[i] = nullptr;
}
if (type == 1){
for (int i = 0; i < 11; i++)
{
alienRow[i] = new Alien(x, y, "Alien1.png");
x = x + 70;
}
}
if (type == 2){
for (int i = 0; i < 11; i++)
{
alienRow[i] = new Alien(x, y, "Alien2.png");
x = x + 70;
}
}
if (type == 3){
for (int i = 0; i < 11; i++)
{
alienRow[i] = new Alien(x, y, "Alien3.png");
x = x + 70;
}
}
}
AlienRow::~AlienRow(){
/*for (int i = 0; i < 11; i++)
{
if (alienRow[i] != nullptr)
{
delete alienRow[i];
}
delete *alienRow;
}*/
delete [] alienRow;
}
void AlienRow::draw(RenderTarget& target, RenderStates states)const{
for (int i = 0; i < 11; i++)
{
target.draw(*alienRow[i]);
}
}
Alien* AlienRow::getAlienRowA(int nr)const{
return alienRow[nr];
}
bool AlienRow::getAlienMove(float x, float y){
for (int i = 0; i < 11; i++)
{
if (alienRow[i]->moveAlien(x, y) == true)
return true;
}
return false;
}
#pragma once
#include "Alien.h"
#include <iostream>
class AlienRow :public sf::Drawable {
private:
Alien* alienRow[11];
float alienVelocity;
public:
Alien* getAlienRowA(int nr)const;
virtual void draw(RenderTarget& target, RenderStates states)const;
bool getAlienMove(float x, float y);
AlienRow(float x, float y, int type);
~AlienRow();
};
Alien:
#include "Alien.h"
#include <iostream>
Alien::Alien(float x, float y, std::string alien){
alienTexture.loadFromFile(alien);
alienSprite.setTexture(alienTexture);
alienSprite.setPosition(x, y);
alienSprite.setScale(sf::Vector2f(0.7f, 0.7f));
}
Alien::~Alien(){
}
void Alien::draw(RenderTarget& target, RenderStates states)const{
target.draw(alienSprite);
}
void Alien::update(float dt){
}
Sprite Alien::getAlienSprite()const{
return alienSprite;
}
void Alien::moveDeadSprite(){
alienSprite.setPosition(alienSprite.getPosition().x, alienSprite.getPosition().y - 700);
}
bool Alien::moveAlien(float x, float y){
alienSprite.move(x, y);
if (alienSprite.getPosition().y > 540){
return true;
}
return false;
}
#pragma once
#include "Entity.h"
class Alien:public Entity{
private:
Sprite alienSprite;
Texture alienTexture;
float velocity;
public:
Sprite getAlienSprite()const;
void moveDeadSprite();
bool moveAlien(float x, float y);
virtual void draw(RenderTarget& target, RenderStates states)const;
virtual void update(float dt);
Alien(float x, float y, std::string alien);
virtual ~Alien();
};
AlienSwarm
#include "AlienSwarm.h"
AlienSwarm::AlienSwarm(float y){
aRow[0] = new AlienRow(0,y,3);
y = y + 50;
aRow[1] = new AlienRow(0,y,2);
y = y + 50;
aRow[2] = new AlienRow(0, y,3);
y = y + 50;
aRow[3] = new AlienRow(0, y,2);
y = y + 50;
aRow[4] = new AlienRow(0, y,1);
y = y + 50;
}
AlienSwarm::~AlienSwarm(){
for (int i = 0; i < 5; i++)
{
delete aRow[i];
}
}
void AlienSwarm::draw(RenderTarget& target, RenderStates states)const{
for (int i = 0; i < 5; i++){
target.draw(*aRow[i]);
}
}
AlienRow* AlienSwarm::getAlienSwarmA(int nr)const{
return aRow[nr];
}
void AlienSwarm::update(){
}
bool AlienSwarm::getAlienMoveRow(){
for (int i = 0; i < 5; i++)
{
if (aRow[i]->getAlienMove(0, 0.5f) == true)
return true;
}
return false;
}
#pragma once
#include "AlienRow.h"
class AlienSwarm:public Drawable{
private:
AlienRow* aRow[5];
float y;
public:
AlienRow* getAlienSwarmA(int nr)const;
bool getAlienMoveRow();
virtual void draw(RenderTarget& target, RenderStates states)const;
virtual void update();
AlienSwarm(float y);
virtual ~AlienSwarm();
};
Match your new calls to your delete calls.
delete *alienRow;
There is no matching new for this. You effectively delete the first element of your array 12 times. Remove this line.
delete [] alienRow;
You array is not created using new. Remove this line as well.
Use smart pointers and (unique_ptr or shared_ptr) and you do not have to delete them.
Related
New to C++. Here is my code:
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
const int Gardensize = 20;//Garden size, a 20X20 2d array
const int initialants = 100;//100 initial ants
const int initialdoodlebug = 5;//5 intial bug
const int antType = 1;//
const int doodleType = 2;//
const char antchar = 'O';//ant will display'O'in the window
const char bugchar = 'X';//
class Garden;
class Organism;
class Ant;
class Doodlebug;
class Garden {
friend class Organism;
friend class Ant;
friend class Doodlebug;
public:
Garden();
~Garden();
int checkType(int x, int y);//check the element type (ant or bug)in the grid
void Display();
private:
Organism grid[Gardensize][Gardensize]; //C2079 'Garden::grid' uses undefined class 'Organism' I have already define the class Organism in advance,have no ideal how to fix this error.
};
Garden::Garden() { //initialize the garden, set all elements in grid to "NULL"
for (int i = 0; i < Gardensize; i++) {
for (int j = 0; j < Gardensize; j++) {
grid[i][j] = NULL; //error:subscript requires array or pointer
}
}
}
Garden::~Garden() {
for (int i = 0; i < Gardensize; i++) {
for (int j = 0; j < Gardensize; j++) {
if (grid[i][j] != NULL) {
grid[i][j] = NULL;
}
}
}
}
void Garden::Display() {
for (int i = 0; i < Gardensize; i++) {
for (int j = 0; j < Gardensize; j++) {
if (grid[i][j].getType == antType) {
cout << antchar;
}
else if (grid[i][j].getType == NULL) {
cout << ".";
}
else if (grid[i][j].getType == doodleType) {
cout << bugchar;
}
}
cout << endl;
}
}
int Garden::checkType(int x, int y) {
return grid[x][y].getType();
}
class Organism {
friend class Garden;
public:
virtual int getType() {}; //
virtual void breed() {};
virtual bool starve() {};
virtual int move( int &breedtoken) {};
protected:
int x = -1; //initial xy place
int y = -1;
Garden garden;
bool moved; //used to define whether org has moved or not
int breedtoken = 0; //used to define whether org need to breed
};
class Ant : public Organism {
public:
Ant() {}; //
Ant(int x, int y, Garden* g)//initial a ant object by define the xy place in the gardene
{
this->x = x;
this->y = y;
garden = *g;
}
~Ant() {};
virtual int getType() {
return antType;
}
virtual int move(int &breedtoken);
virtual void breed() {};
virtual bool starve() { return false; };// ant wont starve
};
int Ant::move(int& breedtoken) {
int dir = rand() % 4;// randomly select direction
switch (dir) {
case 0 :// 0move upwards
if( this->x > 0 && garden.grid[x - 1][y] == NULL ){
garden.grid[x-1][y] = garden.grid[x][y];
garden.grid[x][y] = NULL;
x--;
}
break;
case 1:// 1 move downwards
if (this->x < Gardensize - 1 && garden.grid[x + 1][y] == NULL) {
garden.grid[x + 1][y] = garden.grid[x][y];
garden.grid[x][y] = NULL;
x++;
}
break;
case 2: // 2 move leftwards
if (this->y > 0 && garden.grid[x][y-1] == NULL) {
garden.grid[x][y-1] = garden.grid[x][y];
garden.grid[x][y] = NULL;
y--;
}
break;
case 3: // 3 move to right
if (this->y < Gardensize- 1 && garden.grid[x][y + 1] == NULL) {
garden.grid[x][y + 1] = garden.grid[x][y];
garden.grid[x][y] = NULL;
y++;
}
break;
this->breedtoken += 1;
return breedtoken;
}
}
class Doodlebug :public Organism {
public:
Doodlebug() {};
Doodlebug(int x, int y, Garden* g)
{
this->x = x;
this->y = y;
garden = *g;
}
virtual int getType() {
return doodleType;
}
};
int main()
{
srand(time(NULL));//
Garden garden;
int antCount = 0; //Ant counter, used to intilize 100 ants
int DoodleCount = 0;
Ant antarray[initialants];
Doodlebug doodlebugarray[initialdoodlebug];
while (antCount < initialants) {
int x = rand() % Gardensize;
int y = rand() % Gardensize;
if (garden.checkType(x, y) == NULL) {
antarray[antCount] = Ant(x, y, &garden); //initilize 100 ants
antCount++;
}
}
while (DoodleCount < initialdoodlebug) {
int x = rand() % Gardensize;
int y = rand() % Gardensize;
if (garden.checkType(x, y) == NULL) {
doodlebugarray[DoodleCount] = Doodlebug(x, y, &garden); //用数组的模式创建100只蚂蚁
DoodleCount++;
}
}
garden.Display();//display
}
The project is not finished yet. Right now, the code can initialize 100ants and 5 bugs. It can run properly but keep showing"subscript requires array or pointer " wherever I write grid[i][j] in the for loop. and " 'Garden::grid' uses undefined class 'Organism'" when I define the "Organism grid[][]" in the Garden class. I wonder to know how can i fix these 2 errors, and what's wrong with my 2d array grid?
The problem with the 2d array is caused, because you try to create an array of Organisms, which are up to that point only declared, not defined, and so the compiler doesn't know their size and can't create an array of them. This can b fixed by reordering your classes, or by putting the class declarations in headers. You can also just replace the array with a dynamic array (double pointer), and initialize it after they have been declared.
The other error is just a consequence of the first, fix it and they will both disapear.
You should try reading some book about c or c++ first, and learn a bit about pointers, and design and structure of c++ programs
I know there has been tons of questions like that, but unfortunately after hours of googling and browsing through all of them, none of the answers I read helped. Therefore I am making my own version of my question. The error message I get is: "error: invalid use of incomplete type ‘std::iterator_traits::value_type {aka class Cell}’" My code:
cell.h
#ifndef CELL_H
#define CELL_H
#include <QPushButton>
#include <QMouseEvent>
#include <vector>
class Padding;
class Cell : public QPushButton
{
Q_OBJECT
public:
friend class Padding;
Cell(int x, int y, Padding* padding, QWidget* parent = 0) : QPushButton(parent), x(x), y(y),
padding(padding)
{
setFixedSize(20, 20);
}
Cell(const Cell& object) : QPushButton(), x(object.x), y(object.y), padding(object.padding)
{
setFixedSize(20, 20);
}
int getX() { return x; };
int getY() { return y; };
bool hasMine() { return mine; };
void setHasMine(bool mine) { this -> mine = mine; };
bool isFlagged() { return flagged; };
bool didExplode() { return exploded; };
bool getHasBeenClicked() { return hasBeenClicked; };
void clicked();
~Cell() {};
Cell operator=(const Cell& object)
{
if(&object == this)
{
return *this;
}
padding = object.padding;
x = object.x;
y = object.y;
mine = object.mine;
flagged = object.flagged;
exploded = object.exploded;
hasBeenClicked = object.hasBeenClicked;
setFixedSize(20, 20);
return *this;
}
private:
Padding* padding;
int x;
int y;
bool mine = false;
bool flagged = false;
bool exploded = false;
bool hasBeenClicked = false;
void mousePressEvent(QMouseEvent* e);
void rightClicked();
};
#endif // CELL_H
cell.cpp
#include "cell.h"
#include "padding.h"
void Cell::mousePressEvent(QMouseEvent* event)
{
if(event -> button() == Qt::LeftButton)
{
clicked();
}
else if(event -> button() == Qt::RightButton)
{
rightClicked();
}
}
void Cell::clicked()
{
hasBeenClicked = true;
// TODO: Set the button frame to flat. DONE.
setFlat(true);
// TODO: Make the button not click able. DONE.
setEnabled(false);
// TODO: Display appropriate number on the button, or mine and end the game. DONE.
if(mine)
{
// TODO: Send game over signal and end the game.
//setIcon(QIcon("mine_clicked.png"));
setText("/");
exploded = true;
padding -> gameOver();
}
else
{
setText(QString::number(padding -> countMinesAround(this)));
}
if(padding -> countMinesAround(this) == 0)
{
// Trigger chain reaction; uncover many neighboring cells, if they are not mines.
padding -> triggerChainReactionAround(this);
}
}
void Cell::rightClicked()
{
if(text() != "f")
{
setText("f");
(padding -> minesLeft)--;
}
else
{
setText("");
(padding -> minesLeft)++;
}
flagged = !flagged;
}
padding.h
#ifndef PADDING_H
#define PADDING_H
#include <QWidget>
#include <QGridLayout>
#include <vector>
class Cell;
class Padding : public QWidget
{
Q_OBJECT
public:
friend class Cell;
enum class Difficulty
{
Beginner,
Intermediate,
Advanced,
Custom
};
Padding(QWidget* parent = 0);
void newGame();
void gameOver();
void setLevel(Padding::Difficulty difficulty) { this -> difficulty = difficulty; };
void setPaddingHeight(int height) { paddingHeight = height; };
void setPaddingWidth(int width) { paddingWidth = width; };
void setMines(int mines) { this -> mines = mines; };
int getMinesLeft() { return minesLeft; };
~Padding() {};
private:
struct DifficultyLevelsProperties
{
struct BeginnerProperties
{
const int PADDING_HEIGHT = 9;
const int PADDING_WIDTH = 9;
const int MINES = 10;
} Beginner;
struct IntermediateProperties
{
const int PADDING_HEIGHT = 16;
const int PADDING_WIDTH = 16;
const int MINES = 40;
} Intermediate;
struct AdvancedProperties
{
const int PADDING_HEIGHT = 16;
const int PADDING_WIDTH = 40;
const int MINES = 99;
} Advanced;
} LevelProperties;
Difficulty difficulty = Difficulty::Beginner;
int paddingHeight;
int paddingWidth;
int mines;
// Mines that are not flagged.
int minesLeft;
// Time in seconds since the game was started.
int secondsSinceStart;
std::vector<Cell> cells;
QGridLayout* paddingLayout;
const int CELLS_HEIGHT = 20;
const int CELLS_WIDTH = 20;
int countMinesAround(Cell*);
void triggerChainReactionAround(Cell*);
void updateSecondsSinceStart();
};
#endif // PADDING_H
padding.cpp
#include "padding.h"
#include <QGridLayout>
#include <QTimer>
#include <QTime>
#include <QDebug>
#include "cell.h"
Padding::Padding(QWidget* parent) : QWidget(parent)
{
newGame();
paddingLayout = new QGridLayout(this);
paddingLayout -> setSpacing(0);
}
void Padding::newGame()
{
if(difficulty == Padding::Difficulty::Beginner)
{
paddingHeight = LevelProperties.Beginner.PADDING_HEIGHT;
paddingWidth = LevelProperties.Beginner.PADDING_WIDTH;
mines = LevelProperties.Beginner.MINES;
}
else if(difficulty == Padding::Difficulty::Intermediate)
{
paddingHeight = LevelProperties.Intermediate.PADDING_HEIGHT;
paddingWidth = LevelProperties.Intermediate.PADDING_WIDTH;
mines = LevelProperties.Intermediate.MINES;
}
else if(difficulty == Padding::Difficulty::Advanced)
{
paddingHeight = LevelProperties.Advanced.PADDING_HEIGHT;
paddingWidth = LevelProperties.Advanced.PADDING_WIDTH;
mines = LevelProperties.Advanced.MINES;
}
minesLeft = mines;
cells.clear();
for(int i = 0; i < paddingHeight; i++)
{
for(int j = 0; j < paddingWidth; j++)
{
// TODO: Use smart pointers instead of raw pointers.
Cell* cell = new Cell(j + 1, i + 1, this);
cells.push_back(*cell);
delete cell;
}
}
qsrand(QTime::currentTime().msec());
for(int i = 0; i < mines; i++)
{
// TODO: Fix the randomness of the numbers. DONE.
cells[qrand() % (paddingHeight * paddingWidth) + 1].setHasMine(true);
}
for(int i = 0; i < cells.size(); i++)
{
paddingLayout -> addWidget(&cells[i], cells[i].getY(), cells[i].getX());
}
}
void Padding::gameOver()
{
for(int i = 0; i < cells.size(); i++)
{
cells[i].setEnabled(false);
if((cells[i].hasMine()) && (!cells[i].getHasBeenClicked()))
{
cells[i].clicked();
}
}
}
int Padding::countMinesAround(Cell*)
{
int minesCounter = 0;
for(int i = 0; i < cells.size(); i++)
{
qDebug() << QString::number(cells[i].getX());
if(((x - cells[i].getX() == 0) || (x - cells[i].getX() == 1) || (x -
cells[i].getX() == -1)) && ((y - cells[i].getY() == 0) || (y -
cells[i].getY() == 1) || (y - cells[i].getY() == -1)) &&
(cells[i].hasMine()))
{
minesCounter++;
}
}
return minesCounter;
}
void Padding::triggerChainReactionAround(Cell*)
{
for(int i = 0; i < cells.size(); i++)
{
if(((x - cells[i].getX() == 0) || (x - cells[i].getX() == 1) || (x -
cells[i].getX() == -1)) && ((y - cells[i].getY() == 0) || (y -
cells[i].getY() == 1) || (y - cells[i].getY() == -1)) &&
(!cells[i].getHasBeenClicked()))
{
cells[i].clicked();
}
}
}
Sorry for how long the whole thing, but I could not shorten it as I can't locate what causes the error. Also please ignore any TODO's or any lines that are commented out and I forgot to delete them. Please help!
When you forward declare a type, you can only use pointers or references to that type objects, so this line in padding.h is pretty much not compiling:
std::vector<Cell> cells;
I suppose the compiler complaint comes from where it is trying to decide how to build/destroy a Cell object in a vector. To do that, it needs information about the type, generally from the type declaration (i.e. the header file).
I am creating a Heap type priority queue using a dynamically sized array. I am aware that vectors would be simpler to implement, but this is a learning exercise for me. Everything works great, but I am having issues only when attempting some Unit testing in visual studio '13. I'm experiencing this error
Here is the source file where I attempt to run the Unit tests:
//Prog1Test.cpp
#include "UnitTest.h"
#include <iostream>
int main()
{
PriorityQueue Q = PriorityQueue();
UnitTest::test1(Q);
UnitTest::test2(Q);
UnitTest::test3(Q);
UnitTest::test4(Q);
return 0;
}
Here is the UnitTest.cpp:
//UnitTest.cpp
#include "UnitTest.h"
#include <cassert>
void UnitTest::test1(PriorityQueue Q)
{
Q.clear();
Q.append('a');
Q.append('b');
assert(Q.size() == 2);
assert(Q.check() == true);
}
void UnitTest::test2(PriorityQueue Q)
{
Q.clear();
Q.append('b');
Q.append('a');
assert(Q.size() == 2);
assert(Q.check() == false);
}
void UnitTest::test3(PriorityQueue Q)
{
Q.clear();
Q.insert('a');
Q.insert('b');
assert(Q.size() == 2);
assert(Q.check() == true);
assert(Q.remove() == 'a');
assert(Q.size() == 1);
}
void UnitTest::test4(PriorityQueue Q)
{
Q.clear();
Q.insert('b');
Q.insert('a');
assert(Q.size() == 2);
assert(Q.check() == true);
assert(Q.remove() == 'a');
assert(Q.size() == 1);
}
Here is the UnitTest header file:
//UnitTest.h
#ifndef UnitTest_H
#define UnitTest_H
#include "PriorityQueue.h"
class UnitTest
{
public:
void test1(PriorityQueue Q);
void test2(PriorityQueue Q);
void test3(PriorityQueue Q);
void test4(PriorityQueue Q);
};
#endif
Here is the PriorityQueue class header:
#ifndef PriorityQueue_H
#define PriorityQueue_H
class PriorityQueue
{
private:
char *pq;
int length;
int nextIndex;
char root;
public:
PriorityQueue();
~PriorityQueue();
char& operator[](int index);
void append(char val);
int size();
void clear();
void heapify();
bool check();
void insert(char val);
char remove();
friend class UnitTest;
};
#endif
here is the priorityqueue.cpp file:
#include<math.h>
#include "PriorityQueue.h"
PriorityQueue::PriorityQueue()
{
pq = new char[0];
this->length = 0;
this->nextIndex = 0;
}
PriorityQueue::~PriorityQueue() {
delete[] pq;
}
char& PriorityQueue::operator[](int index) {
char *pnewa;
if (index >= this->length) {
pnewa = new char[index + 1];
for (int i = 0; i < this->nextIndex; i++)
pnewa[i] = pq[i];
for (int j = this->nextIndex; j < index + 1; j++)
pnewa[j] = 0;
this->length = index + 1;
delete[] pq;
pq = pnewa;
}
if (index > this->nextIndex)
this->nextIndex = index + 1;
return *(pq + index);
}
void PriorityQueue::append(char val) {
char *pnewa;
if (this->nextIndex == this->length) {
this->length = this->length + 1;
pnewa = new char[this->length];
for (int i = 0; i < this->nextIndex; i++)
pnewa[i] = pq[i];
for (int j = this->nextIndex; j < this->length; j++)
pnewa[j] = 0;
delete[] pq;
pq = pnewa;
}
pq[this->nextIndex++] = val;
}
int PriorityQueue::size() {
return this->length;
}
void PriorityQueue::clear() {
delete[] pq;
pq = new char[0];
this->length = 0;
this->nextIndex = 0;
}
void PriorityQueue::heapify() {
char parent;
char root;
char temp;
for (double i = this->length - 1; i >= 0; i--)
{
root = pq[0];
int parentindex = floor((i - 1) / 2);
int leftchildindex = 2 * i + 1;
int rightchildindex = 2 * i + 2;
if (pq[(int)i] <= pq[leftchildindex] && pq[(int)i] <= pq[rightchildindex])
{
pq[(int)i] = pq[(int)i];
}
else if (rightchildindex < this->length && pq[(int)i] > pq[rightchildindex])
{
temp = pq[(int)i];
pq[(int)i] = pq[rightchildindex];
pq[rightchildindex] = temp;
heapify();
}
else if (leftchildindex < this->length && pq[(int)i] > pq[leftchildindex])
{
temp = pq[(int)i];
pq[(int)i] = pq[leftchildindex];
pq[leftchildindex] = temp;
heapify();
}
}
}
void PriorityQueue::insert(char val) {
char *pnewa;
if (this->nextIndex == this->length) {
this->length = this->length + 1;
pnewa = new char[this->length];
for (int i = 0; i < this->nextIndex; i++)
pnewa[i] = pq[i];
for (int j = this->nextIndex; j < this->length; j++)
pnewa[j] = 0;
delete[] pq;
pq = pnewa;
}
pq[this->nextIndex++] = val;
PriorityQueue::heapify();
}
bool PriorityQueue::check() {
char root;
root = pq[0];
for (int i = this->length - 1; i >= 0; i--)
{
if ((int)pq[i]< (int)root)
return false;
}
return true;
}
char PriorityQueue::remove() {
char root = pq[0];
char *qminus;
qminus = new char[this->length];
for (int i = 1; i<this->length; i++)
qminus[i - 1] = pq[i];
pq = qminus;
this->length -= 1;
PriorityQueue::heapify();
return root;
}
you need to declare your test methods as static
class UnitTest
{
public:
static void test1(PriorityQueue Q);
static void test2(PriorityQueue Q);
static void test3(PriorityQueue Q);
static void test4(PriorityQueue Q);
};
Note that the static methods can only refer to static data members, since there is no class instance when calling these methods.
you need an instance of UnitTest
PriorityQueue Q = PriorityQueue();
UnitTest t;
t.test1(Q);
t.test2(Q);
t.test3(Q);
t.test4(Q);
return 0;
Note that currently there's no good reason why your test functions are part of a class altogether.
Both previous answers are correct, a static or using object may solve the problem.
Another solution is to use namespace instead of class:
namespace UnitTest
{
void test1(PriorityQueue Q);
void test2(PriorityQueue Q);
void test3(PriorityQueue Q);
void test4(PriorityQueue Q);
};
I'm doing a phone registry and in it you need to be able to add, remove and show the phones on stock. I've made it possible to add in phones but whenever I add let's say 3 phones and remove the second one then both the third and second phone are deleted and I don't understand why.
This is my CellPhoneHandler.h file:
#ifndef CELLPHONEHANDLER_H
#define CELLPHONEHANDLER_H
#include "CellPhone.h"
class CellPhoneHandler
{
private:
CellPhone **phone;
int nrOfPhones;
int priceOfPhone;
int stockCapacity;
int nrOfPhonesInArr;
public:
CellPhoneHandler();
~CellPhoneHandler();
void addPhone(string brand, int nrOf, int price);
bool removePhoneFromStock(string name, int nrOf);
int getNrOfPhones() const;
int getNrOfPhonesInArr() const;
int getPrice() const;
void getPhonesAsString(string arr[], int nrOf, int priceOfPhone) const;
};
#endif // !CELLPHONEHANDLER_H
this is my CellPhoneHandler.cpp file.
#include "CellPhoneHandler.h"
CellPhoneHandler::CellPhoneHandler()
{
this->phone = nullptr;
this->nrOfPhones = 0;
this->priceOfPhone = 0;
this->stockCapacity = 0;
this->nrOfPhonesInArr = 0;
}
CellPhoneHandler::~CellPhoneHandler()
{
for (int i = 0; i < nrOfPhonesInArr; i++)
{
delete phone[i];
}
delete[] phone;
}
void CellPhoneHandler::addPhone(string brand, int nrOf, int price)
{
if (stockCapacity < nrOfPhonesInArr + 1)
{
CellPhone ** tempArray = new CellPhone*[this->nrOfPhonesInArr + 1];
for (int i = 0; i < nrOfPhonesInArr; i++)
{
tempArray[i] = this->phone[i];
}
delete[] this->phone;
this->phone = tempArray;
this->phone[this->nrOfPhonesInArr] = new CellPhone(brand, nrOf, price);
this->nrOfPhonesInArr++;
//this->stockCapacity++;
}
}
bool CellPhoneHandler::removePhoneFromStock(string name, int nrOf)
{
bool phoneFound = false;
int index = nrOfPhonesInArr;
for (int i = 0; i < nrOfPhonesInArr; i++)
{
if (this->phone[i]->getBrand() == name);
{
index = i;
phoneFound = true;
this->nrOfPhonesInArr--;
}
}
if (phoneFound == true)
{
delete phone[index];
phone[index] = nullptr;
}
return phoneFound;
}
int CellPhoneHandler::getNrOfPhones() const
{
return this->nrOfPhones;
}
int CellPhoneHandler::getNrOfPhonesInArr() const
{
return this->nrOfPhonesInArr;
}
int CellPhoneHandler::getPrice() const
{
return this->priceOfPhone;
}
void CellPhoneHandler::getPhonesAsString(string arr[], int nrOf, int priceOfPhone) const
{
for (int i = 0; i < nrOf; i++)
{
arr[i] = this->phone[i]->toString();
}
}
The problem is caused by an unwanted ;.
if (this->phone[i]->getBrand() == name); // if ends here.
The next block is executed for all items.
{
index = i;
phoneFound = true;
this->nrOfPhonesInArr--;
}
Remove that ; in the if line.
when I try to set
cub.SetArray(cube);
I get an error
Console Application1.exe has triggered a breakpoint
What I'm doing wrong? When I try to debug cub -> cubesarray I get size -842150451. I don't understand why.Here's my all code
class Cube{
public:
static const int Change_ARRAY = 5;
private:
string color;
int size;
int *walls;
int n; // current size of array
int maximumsize; // maximum size of array
void Increase(int many);
public:
Cube(int maximumsize = 0);
~Cube();
void SetWalls(int wall);
void SetColor(string color);
void SetSize(int size);
string GetColor(){return color;}
int GetWalls(int i){return walls[i];}
int GetSize(){return size;}
int GetN(){return n;}
};
Cube::Cube(int maximumsize):n(0), maximumsize(maximumsize), size(size), walls(NULL){
if(maximumsize > 0){
walls = new int[maximumsize];
}
}
Cube::~Cube(){
if(walls){
delete [] walls;
}
}
void Cube::Increase(int many){
if(many > maximumsize){
int *newest = new int[many];
for(int i=0; i<n; i++)
newest[i] = walls[i];
delete [] walls;
walls = newest;
maximumsize = many;
}else if( many < maximumsize){
int *newest = new int[many];
for(int i=0; i<many; i++)
newest[i] = walls[i];
delete [] walls;
walls = newest;
n = maximumsize = many;
}
}
void Cube::SetWalls(int wall){
if(n == maximumsize) Increase(n + Change_ARRAY);
walls[n] = wall;
n++;
}
void Cube::SetColor(string color){
this->color = color;
}
void Cube::SetSize(int size){
this->size = size;
}
class CubesArray{
public:
static const int Change_Array = 5;
private:
Cube *cubesarray;
int currentsize; // current size of array
int maxsize; // maximumsize
void Change (int kk);
public:
CubesArray(int maxsize = 1);
~CubesArray();
void SetArray(Cube c);
Cube GetArray(int ind){return cubesarray[ind];}
int GetCsize(){return currentsize;}
};
CubesArray::CubesArray(int maxsize):cubesarray(NULL), currentsize(0), maxsize(maxsize){
if(maxsize > 0){
cubesarray = new Cube[maxsize];
}
}
CubesArray::~CubesArray(){
if(cubesarray){
delete [] cubesarray;
}
}
void CubesArray::Change(int kk){
if(kk > maxsize){
Cube *newarr = new Cube[kk];
for(int i=0; i<currentsize; i++)
newarr[i] = cubesarray[i];
delete [] cubesarray;
cubesarray = newarr;
maxsize = kk;
}if(kk < maxsize){
Cube *newarr = new Cube[kk];
for(int i=0; i<kk; i++)
newarr[i] = cubesarray[i];
delete [] cubesarray;
cubesarray = newarr;
currentsize = maxsize = kk;
}
}
void CubesArray::SetArray(Cube cub){
if(currentsize = maxsize) Change(currentsize + Change_Array);
cubesarray[currentsize] = cub;
currentsize++;
}
void Read(CubesArray & cub);
int main(){
CubesArray cub;
Read(cub);
system("pause");
return 0;
}
void Read(CubesArray & cub){
string color;
int size;
int i=0;
Cube cube;
ifstream fd(Data);
while(!fd.eof()){
fd >> color >> size;
cube.SetSize(size);
cube.SetColor(color);
cout << cube.GetColor() << " " << cube.GetSize() << " ";
while(fd.peek() != '\n' && !fd.eof()){
int w;
fd >> w;
cube.SetWalls(w);
cout << cube.GetWalls(i) << " ";
cub.SetArray(cube); // when I set cube to cub I get this error!!!
i++;
}
cout << endl;
fd.ignore();
}
}
Change:
if(currentsize = maxsize)
To:
if(currentsize == maxsize)
In addition, here is your real problem:
You have no copy-constructor in class Cube, so the walls array is not properly copied whenever you send a Cube instance by value, e.g., cub.SetArray(cube).
You must define it as follows:
Cube::Cube(const Cube& cube):n(cube.n),maximumsize(cube.maximumsize),size(cube.size),wall(NULL)
{
if (maximumsize > 0)
{
walls = new int[maximumsize];
for (int i=0; i<maximumsize; i++)
wall[i] = cube.wall[i];
}
}
And you have no assignment-operator in class Cube, so the walls array is not properly copied whenever you assign one Cube instance into another, e.g., cubesarray[currentsize] = cub.
You must define it as follows:
Cube& Cube::operator=(const Cube& cube)
{
n = cube.n;
maximumsize = cube.maximumsize;
size = cube.size;
wall = NULL;
if (maximumsize > 0)
{
walls = new int[maximumsize];
for (int i=0; i<maximumsize; i++)
wall[i] = cube.wall[i];
}
return *this;
}
BTW, in the copy-constructor, you can simply call the assignment-operator (remove coding redundancy):
Cube::Cube(const Cube& cube)
{
if (this != &cube)
*this = cube;
}
Your Cube class violates the rule of three. Look here:
void CubesArray::SetArray(Cube cub){ // calls copy constructor
That call creates a copy of your Cube class. Your Cube class is not safely copyable. Please see this and scroll down to the Managing Resources section: What is The Rule of Three?
You should pass Cube by reference or const reference, not by value. Doing so may correct the error you're having now, but still, your class is faulty.