Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


#Actualsooner123

Posted 02 December 2012 - 02:44 PM

Here is my code. I'm having two issues with destructors here.

I'm trying to write a destructor that will run through the linked list of children of the current node being destroyed and delete all of them (which should recursively call their destructors as well)

Instead the program just crashes.

The other issue is that this if I uncomment the code in RemoveAndDeleteAllWidgets, and try to manually call the destructor, I get an error saying 'no match for 'operator~' in '~Widget()'

This last error is making me suspect that my compiler might be buggy and have issues dealing with destructors.

  ~Widget()
  {
   cout << "children of node with value " << x << " being deleted" << endl;
   Node*sentinel = new Node;
   sentinel = list;
   cout << sentinel->data->x << "*" << endl;
   while (sentinel != NULL)
   {
	sentinel = sentinel->next;
	delete sentinel->prev;
   }
  }
  void RemoveAndDeleteAllWidgets()
  {
   //~Widget();
  }

#2sooner123

Posted 02 December 2012 - 01:35 PM

Here is my code. I'm having two issues with destructors here.

I'm trying to write a destructor that will run through the linked list of children of the current node being destroyed and delete all of them (which should recursively call their destructors as well)

Instead the program just crashes.

The other issue is that this if I uncomment the code in RemoveAndDeleteAllWidgets, and try to manually call the destructor, I get an error saying 'no match for 'operator~' in '~Widget()'

This last error is making me suspect that my compiler might be buggy and have issues dealing with destructors.

#include <iostream>
using namespace std;
class Widget;
struct Node
{
Node* prev;
Node* next;
Widget* data;
};
class Widget
{
public:
  int x, y, w, h;
  Node* list;
  void AddWidget(Widget* obj)
  {
   Node*elem = new Node;
   elem->prev = NULL;
   elem->data = obj;
   if (list == NULL)
   {
	elem->next = NULL;
	list = elem;
   }
   else
   {
	elem->next = list;
	list->prev = elem;
	list = elem;
   }
  }
  void RemoveWidget(Widget* obj)
  {
   for(Node*elem = list; elem != NULL; elem = elem->next)
   {
	if (elem->data == obj)
	{
	 //if node to delete is head of list
	 if (elem->prev == NULL)
	 {
	  list = elem->next;
	 }
	 //if node to delete is tail of list
	 else if (elem->next == NULL)
	 {
	  elem->prev->next = NULL;
	 }
	 //if node to delete is not start or end
	 else
	 {
	  elem->prev->next = elem->next;
	  elem->next->prev = elem->prev;
	 }
	 delete elem;
	 return;
	}
   }
  }
  ~Widget()
  {
   cout << "children of node with value " << x << " being deleted" << endl;
   Node*sentinel = new Node;
   sentinel = list;
   cout << sentinel->data->x << "*" << endl;
   while (sentinel != NULL)
   {
	sentinel = sentinel->next;
	delete sentinel->prev;
   }
  }
  void RemoveAndDeleteAllWidgets()
  {
   //~Widget();
  }
  void DebugInfo(int tabDepth=0)
  {
   for (int i=0; i<tabDepth; i++) cout << "	";
   cout << "child: " << x << endl;
   Node*elem = new Node;
   elem = list;
   while(elem != NULL)
   {
	elem->data->DebugInfo(tabDepth+1);
	elem = elem->next;
   }
  }
  Widget()
  {
   list = NULL;
  }
};
class PanelWidget: public Widget
{
public:
  PanelWidget(int a, int b, int c, int d)
  {
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
class ButtonWidget: public Widget
{
public:
  bool enabled;
  const char* text;
  ButtonWidget(const char* e, int a, int b, int c, int d)
  {
   text = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
  void SetEnabled(bool b)
  {
   enabled = b;
  }
};
class DialogWidget: public Widget
{
public:
  const char* header;
  DialogWidget(const char* e, int a, int b, int c, int d)
  {
   header = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
class PictureWidget: public Widget
{
public:
  const char* img;
  PictureWidget(const char* e, int a, int b, int c, int d)
  {
   img = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
  
int main()
{
Widget* mainScreen = new PanelWidget(0,0,800,600);
Widget* background = new PictureWidget("background.jpg", 0, 0, 800, 600);
Widget* icon = new PictureWidget("icon.jpg", 10, 10, 50, 50 );
mainScreen->AddWidget(background);
mainScreen->AddWidget(icon);
Widget* dialog = new DialogWidget("Quit", 100, 50, 200, 200);
ButtonWidget* okButton = new ButtonWidget("OK", 150, 50, 50, 20);
ButtonWidget* cancelButton = new ButtonWidget("Cancel", 150, 150, 50, 20);
dialog->AddWidget(cancelButton);
dialog->AddWidget(okButton);
dialog->RemoveWidget(okButton);
dialog->AddWidget(okButton);
mainScreen->AddWidget(dialog);
okButton->SetEnabled( true );
cancelButton->SetEnabled( true );
mainScreen->DebugInfo();
mainScreen->RemoveAndDeleteAllWidgets();
delete mainScreen;
}

#1sooner123

Posted 02 December 2012 - 01:34 PM

Here is my code. I'm having two issues with destructors here.

I'm trying to write a destructor that will run through the linked list of children of the current node being destroyed and delete all of them (which should recursively call their destructors as well)

Instead the program just crashes.

The other issue is that this if I uncomment the code in RemoveAndDeleteAllTestXs, and try to manually call the destructor, I get an error saying 'no match for 'operator~' in '~TestX()'

This last error is making me suspect that my compiler might be buggy and have issues dealing with destructors.

#include <iostream>
using namespace std;
class Widget;
struct Node
{
Node* prev;
Node* next;
Widget* data;
};
class Widget
{
public:
  int x, y, w, h;
  Node* list;
  void AddWidget(Widget* obj)
  {
   Node*elem = new Node;
   elem->prev = NULL;
   elem->data = obj;
   if (list == NULL)
   {
    elem->next = NULL;
    list = elem;
   }
   else
   {
    elem->next = list;
    list->prev = elem;
    list = elem;
   }
  }
  void RemoveWidget(Widget* obj)
  {
   for(Node*elem = list; elem != NULL; elem = elem->next)
   {
    if (elem->data == obj)
    {
	 //if node to delete is head of list
	 if (elem->prev == NULL)
	 {
	  list = elem->next;
	 }
	 //if node to delete is tail of list
	 else if (elem->next == NULL)
	 {
	  elem->prev->next = NULL;
	 }
	 //if node to delete is not start or end
	 else
	 {
	  elem->prev->next = elem->next;
	  elem->next->prev = elem->prev;
	 }
	 delete elem;
	 return;
    }
   }
  }
  ~Widget()
  {
   cout << "children of node with value " << x << " being deleted" << endl;
   Node*sentinel = new Node;
   sentinel = list;
   cout << sentinel->data->x << "*" << endl;
   while (sentinel != NULL)
   {
    sentinel = sentinel->next;
    delete sentinel->prev;
   }
  }
  void RemoveAndDeleteAllWidgets()
  {
   //~Widget();
  }
  void DebugInfo(int tabDepth=0)
  {
   for (int i=0; i<tabDepth; i++) cout << "\t";
   cout << "child: " << x << endl;
   Node*elem = new Node;
   elem = list;
   while(elem != NULL)
   {
    elem->data->DebugInfo(tabDepth+1);
    elem = elem->next;
   }
  }
  Widget()
  {
   list = NULL;
  }
};
class PanelWidget: public Widget
{
public:
  PanelWidget(int a, int b, int c, int d)
  {
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
class ButtonWidget: public Widget
{
public:
  bool enabled;
  const char* text;
  ButtonWidget(const char* e, int a, int b, int c, int d)
  {
   text = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
  void SetEnabled(bool b)
  {
   enabled = b;
  }
};
class DialogWidget: public Widget
{
public:
  const char* header;
  DialogWidget(const char* e, int a, int b, int c, int d)
  {
   header = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
class PictureWidget: public Widget
{
public:
  const char* img;
  PictureWidget(const char* e, int a, int b, int c, int d)
  {
   img = e;
   x = a;
   y = b;
   w = c;
   h = d;
  }
};
  
int main()
{
Widget* mainScreen = new PanelWidget(0,0,800,600);
Widget* background = new PictureWidget("background.jpg", 0, 0, 800, 600);
Widget* icon = new PictureWidget("icon.jpg", 10, 10, 50, 50 );
mainScreen->AddWidget(background);
mainScreen->AddWidget(icon);
Widget* dialog = new DialogWidget("Quit", 100, 50, 200, 200);
ButtonWidget* okButton = new ButtonWidget("OK", 150, 50, 50, 20);
ButtonWidget* cancelButton = new ButtonWidget("Cancel", 150, 150, 50, 20);
dialog->AddWidget(cancelButton);
dialog->AddWidget(okButton);
dialog->RemoveWidget(okButton);
dialog->AddWidget(okButton);
mainScreen->AddWidget(dialog);
okButton->SetEnabled( true );
cancelButton->SetEnabled( true );
mainScreen->DebugInfo();
mainScreen->RemoveAndDeleteAllWidgets();
delete mainScreen;
}

PARTNERS