I was trying to do this program when I noticed in my points3D method that something was causing a Segmentation fault and I don't know what caused it. The goal of this program is to test out 4 different methods and try to get a certain answer. I thought I mostly did it right until I noticed my method ~point3D. Although I fixed this problem, I cant find any problem why this would happen.
//You only need iostream, no other header files are allowed
#include <iostream>
using namespace std;
//Code for part 1
class point2D {
int x, y;
char* label;
public:
int getX(){
return x;
}
int getY(){
return y;
}
char* getLabel(){
return label;
}
void setX(int a){
x = a;
}
void setY(int b){
y = b;
}
void setLabel(char* g){
label = g;
}
virtual void show(){
cout << "The point data is " << "(" << x << "," << y << "," << label << ")" << endl;
}
point2D(int x, int y, char * label){
this->x = x;
this->y = y;
label = nullptr;
setLabel(label);
};
~point2D(){
if(label != nullptr){
delete[] label;
}
};
};
//Code for part 2
class point3D : public point2D {
protected:
int z;
public:
int getZ(){
return z;
}
void setZ(int c){
z = c;
}
void show(){
cout << "(" << getX() << "," << getY() << "," << z << "," << getLabel() << ")\n";
};
point3D(int a, int b, int c, char * l) : point2D(a,b,l) {
z = c;
};
~point3D();
};
/*
//Code for Part 3
class node {
public:
point3D* data;
node* next;
static int counter;
void show(){
if(data != nullptr){
data->show();
}
}
node(){
data = nullptr;
next = nullptr;
}
~node(){
if(data != nullptr){
delete[] data;
}
}
};
//Code for Part 4
class linkedList {
node* head;
public:
bool empty() {
return head == nullptr;
}
int size() {
int c = 0;
node* it = head;
while (it != nullptr) {
c++;
it = it->next;
}
return c;
}
bool push(point3D* np) {
node* nn = new node();
nn->data = np;
return push(nn);
}
bool push(node* nn) {
nn->next = head;
head = nn;
return true;
}
bool append(point3D* np) {
node* nn = new node();
nn->data = np;
return append(nn);
}
bool append(node* nn) {
if (head != nullptr) {
node* it = head;
while (it->next != nullptr) {
it = it->next;
}
it->next = nn;
}
else {
head = nn;
}
return true;
}
void show() {
node* it = head;
while (it != nullptr) {
it->show();
it = it->next;
}
}
linkedList() {
head = nullptr;
}
~linkedList(){
node * it = head;
node = nullptr;
while(it != nullptr){
}
}
};
int node::counter = 0;
*/
int main() {
int testMode;//Determines the test to be executed
std::cin >> testMode;
switch (testMode) {
case 1:{
//1) Test part 1
point2D * p12D = new point2D(12, 24, (char*)"test 1");
p12D->setX(25);
p12D->setLabel((char*)"Landing Zone A");
p12D->show();
delete p12D;
break;
}
case 2: {
//2) Test part 2
point3D* p13D = new point3D(12, 24, 36,(char*) "test 2");
p13D->setY(50);
p13D->setLabel((char*)"Landing Zone B");
p13D->show();
point2D* p22D = dynamic_cast<point2D*>(p13D);
p22D->show();
std::decay_t(&p13D);
break;
}
/*
case 3: {
//3) Test part 3
node* nn = new node();
std::cout << node::counter << std::endl;
delete nn;
nn = new node();
std::cout << node::counter << std::endl;
point3D* p23D = new point3D(48,60,72,"Landing Zone C");
nn->data = p23D;
nn->show();
delete nn;
break;
}
case 4: {
//4) Test part 4
linkedList* myList = new linkedList();
int i;
for (i = 0; i < 3; i++) {
node* nn = new node();
point3D* p3D = new point3D(12*i, 24*i, 36*i, "Landing Zone ");
myList->addNode(p3D);
}
myList->show();
point3D* p3D = new point3D(12 * i, 24 * i, 36 * i, "Landing Zone ");
myList->insertNode(p3D, 1);
myList->show();
delete myList;
break;
}
*/
}
return 0;
}
I have tried multiple things and got a error that was void operator delete(void*, std::size_t)’ called on unallocated object ‘p13D.
Aucun commentaire:
Enregistrer un commentaire