• Advertisement

Archived

This topic is now archived and is closed to further replies.

Class Destructor Problem in C++

This topic is 5552 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm kinda stuck on this one... Maybe somebody here can help me? I wrote this destructor for my TextBoxControl class, which is supposed to delete all of the dynamically allocated TextBox structs I've created in the program. But it won't compile! I don't know what's wrong. The error message I get says "left of '.boxtype' must have class/struct/union type" but the argument to the left of .boxtype IS a pointer to a struct. Here's the code:
TextBoxControl::~TextBoxControl()
{
	// Delete Dynamically Allocated Variables

	TextBox *pModifybox=startbox.nextbox;
	TextBox *pNextbox=NULL;
	for(;
	{
		if(*pModifybox.boxtype==1) break;
		*pNextbox=*pModifybox.nextbox;
		delete *pModifybox;
		pModifybox=pNextbox;
	}
} 
If anybody can see what I'm doing wrong, I'd be greatful for some help. P.S. the infinate for loop appears as a smiley. How irritating. It should be for( ; ; ) [edited by - Relee on December 6, 2002 6:44:42 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The problem is this:

*pModifybox.boxtype


this equates to *(pModifybox.boxtype)
so the compiler is complaining because you presumably must use -> on pModifybox.

Try:

if ((*pModifybox).boxtype==1) break;

Share this post


Link to post
Share on other sites
// Delete Dynamically Allocated Variables

TextBox *pModifybox=startbox.nextbox;
TextBox *pNextbox=NULL;

for(; {
if(*pModifybox.boxtype==1) break; *pNextbox=*pModifybox.nextbox;
delete *pModifybox;
pModifybox=pNextbox;
}

I hardly know where to begin! Firstly, don''t use an infinite loop here, it makes more sense to use a while loop. Secondly, you don''t seem to know how to use pointers in C\C++ at all - perhaps you should be working on something less complex than a linked list of text boxes? (is this homework by any chance?).

How about this?

TextBox *pModifybox = startbox.nextbox, *pNextbox;

while ( pModifybox != 0)
{
if ( pModifybox->boxtype == 1 )
break;
pNextbox = pModifybox->nextbox;
delete ( pModifyBox );
pModifybox = pNextbox;
}

I''m not sure why you are breaking the loop if boxtype == 1. You should only break the loop when you reach the end of the list, ie. when .nextbox = 0 (it does = 0 at the end of the list, doesn''t it?).

Finally, it goes without saying that you should use std::list for this kind of thing (perhaps). It does all the hard work, so you don''t have to!

Share this post


Link to post
Share on other sites
Well the project I'm working on is for my own benefit, it's a learning experience.

I am a bit new at using pointers in C++ but all the other pointers I've used have worked fine. 'sides a Linked List isn't that complicated.

I'll try out what you guys suggested. Thanks for your help!


Oh and thanks for the tip about using the while loop, I don't know why I didn't think of that! I have a lot of loops that do that and I'll switch them to while loops right away!


Finally the linked list starts with a box of type 0 and ends with a box of type 1. The boxes in between are type 2. If it finds a box of type 1 then it ends the loop because it's gone through the whole list. Once I've gotten it working I'm going to add more types, to represent different styles of boxes.

Thanks again for your help!


Edit: actually the first box's lastbox pointer points to the first box again, and the last box's nextbox pointer points to the last box again.

Edit 2: Oh and one more thing, I noticed you used the command delete ( pModifyBox );

When I used delete *pModifybox I wasn't totally sure it would work. I wasn't able to find any information in my documentation about using delete on dynamically allocated structs through a pointer.

If it's no trouble could you tell me what I had done wrong exactly?

[edited by - Relee on December 6, 2002 7:14:07 PM]

[edited by - Relee on December 6, 2002 7:26:13 PM]

Share this post


Link to post
Share on other sites
I changed the function to

TextBoxControl::~TextBoxControl()
{
// Delete Dynamically Allocated Variables

TextBox *pModifybox=startbox.nextbox;
TextBox *pNextbox=NULL;
while(*pModifybox->boxtype!=1)
{
pNextbox=*pModifybox->nextbox;
delete (pModifybox);
pModifybox=pNextbox;
}
}


But now I get an illegal indirection error on the line with the while statement. I also changed my other pointers to match the -> format and changed most of my for loops to while loops, but that seems to have caused 90 errors.


Perhaps there''s something missing? While I wait for any replies I''ll take a look through my documentation and such to try and figure out what I''m doing wrong...

Share this post


Link to post
Share on other sites
quote:
Original post by Relee
now I get an illegal indirection error on the line with the while statement.

That''s because you have an extra dereference that you don''t need. "->" is the member pointer dereference operator. It dereferences pModifybox for you.

Share this post


Link to post
Share on other sites
lol oh! you're right. Thank you for your help.


edit: Oh hey my program compiled, and worked! Sort-of. Thanks for your help everybody! Now I just have to figure out some logic errors I made.

[edited by - Relee on December 6, 2002 10:34:33 PM]

Share this post


Link to post
Share on other sites
I highly recommend you make your linked list NOT be circular the first time (don''t make last point to first) ...

when you get it working as a strait list ... THEN make it circular (cause making sure your iteration and delete logic is right for circular lists is slightly harder) ...

Share this post


Link to post
Share on other sites

  • Advertisement