Results 1 to 18 of 18

Thread: Destructors

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Destructors

    It's me again.

    Having had trouble with converting an implimentation of a Linked List that stored integers to one that stored dynamic types, i decided to rewrite it from scratch.
    I think i've layed out my code a lot better this time, but i'm not quite sure what i should put in the destructors.
    I'd greatly appreciate it if someone could give me some pointers.

    I've attached the cpp file, as it's a bit big to copy & paste (and the formatting's horrible).

    P.s. at the moment it's a stack, but i'm probably gonna change it so it's a base class, and have stacks, queues and ordered lists inheriting from it (thanks again Cornedbee).
    Attached Files Attached Files
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Much better

    Head and Tail should be private or protected. protected if you want to derive classes.

    If you derive publicly, make the destructor virtual.

    The destructor's purpose is to make sure that no nodes remain. It should go through the list and delete every node.

    The data parameters should be const references if you've learned about them. Both Pop and Poke have control paths that don't return values (if Head == NULL), they should do something. Return a default object or, better, throw an exception.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Ok, thanks for the comments/help. I'll make the changes and look up how to raise an exception.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Ok, i've tried to get inheritance to work and i think i'm almost there, except that i can't get the base class public methods (pop, poke and PrintList) to get recognized in the derived classes.

    I've attached the .h file and i'd greatly appreciate it if someone could take a look.

    Filename changed for portability (and Cornedbee )
    Attached Files Attached Files
    Last edited by SLH; Dec 8th, 2003 at 02:45 PM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Would you mind using a filename that doesn't contain whitespace? Not only is whitespace bad for portability, it also screws my direct download-and-view system up.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by CornedBee
    Would you mind using a filename that doesn't contain whitespace? Not only is whitespace bad for portability, it also screws my direct download-and-view system up.
    It's the least i could do (see above post).
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You don't need to redeclare the functions.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Do you meen i write the functions in the base class, then don't mention them in the derived classes?

    If so i already tried that, but it didn't work i still get this error message (for each method):

    error C2662: 'cLL<TData>::Pop' : cannot convert 'this' pointer from 'cStack<TData>' to 'cLL<TData> &' with [TData=int]

    I don't think it should make a difference, but here's how i'm calling the functons:

    Code:
    #include "Linked List.h"
    
    void main()
    {
    	cStack <int> aStack;
    	aStack.Push(1);
    	aStack.Push(2);
    	aStack.Push(3);
    	aStack.Pop();
    	aStack.Push(aStack.Poke());
    	aStack.PrintList();
    }
    Thanks for your help.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try changing the subclass start lines from
    template <class TData> class cQueue : public cLL
    to
    template <class TData> class cQueue : public cLL<TData>
    .

    I'm not sure if that changes anything, but it's worth a try.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Thanks for the reply. I made the change, but this time it gives the following errors:

    When i call the stack's pop procedure, like this:
    Code:
    aStack.Pop();
    It gives this error message:

    error C2027: use of undefined type 'TData'

    and when i call the push procedure (with the poke procedure) like this:

    Code:
    aStack.Push(aStack.Poke());
    It gives this error message:

    error C2664: 'cStack<TData>::Push' : cannot convert parameter 1 from 'TData' to 'const long' with [TData=long]


    I think it's not replacing the <TData> with the appropriate data type in the inherited functions for some reason, but i'm not sure how to resolve this.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  11. #11

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Unhappy

    Bump!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    This is somewhat absurd...

    Can you upload the file with all recent changes? Then I can try compiling it myself.

    And no spaces
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Sure, here's the linked list h file.
    Attached Files Attached Files
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  14. #14

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    and here's the cpp with the calls in it.

    I use MS Visual Studio .Net 2003 to try and compile it btw.
    Attached Files Attached Files
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You have various minor errors and problems, but the prominent thing is that you misinterpreted my last advice.

    Try changing the subclass start lines from
    template <class TData> class cQueue : public cLL
    to
    template <class TData> class cQueue : public cLL<TData>
    Compare my final line to your result:
    Code:
    template <class TData> class cQueue : public cLL<class TData>
    This is your main problem.

    Others are that you catch the exceptions too early (doesn't make sense - why raise them in the first place if you do it that way?) and that your catches are not followed by blocks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Cheers for your continued help!
    I've been able to fix the my code as per your help, but i'm not sure what you meen about the exceptions. I had a look for a 'block' command but couldn't find anything.
    I also had a look at MS' try...catch examples, but they all have examples where the carch directly follows the try, so i'm not sure where a good place for it to go would be.

    Thanks for any reply.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Read up on the use of exceptions, how and especially why they get used and you'll see what I mean.

    As for the block thing, all I'm saying is that VC++ gives me an error for
    Code:
    try {
      ...
    } catch(type)
      cerr << "Error";
    but not for
    Code:
    try {
      ...
    } catch(type) {
      cerr << "Error";
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Ok, i'll have a look for the use of exceptions and why they're used.
    I misunderstood what you ment about a 'block' statement, but managed to figure the errors out anyway. Thanks for the clarification though.

    Your help is much appreciated.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width