Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: c++ halp

  1. #1
    The Original Reputation: 1538 PrivateParts's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    4,814

    Default c++ halp

    ok. So im having a problem with a destructor in c++

    I have a myString class. Which uses character arrays to create strings.
    it has overloaded operators to do manipulation on character arrays.

    in myString class i have private member variables int _length which gets the length of a dynamically allocated array, and a char *_ptr which points to the char array

    So the class isnt doing much. the destructor just does

    delete [] _ptr;
    _ptr = NULL;



    Now. I have a CProduct class which uses myString.
    The private member variables in it are myString _id, myString _name, etc

    I need a destructor in this class too.

    i am trying to do

    delete [] _id;

    etc etc in this destructor. But i get a error:

    Error 1 error C2440: 'delete' : cannot convert from 'myString' to 'void *' o:\comp 252\labs\lab10\mystring\mystring\cproduct.cpp 73


    I cant figure out how to fix this.. havent seen an mention of it in the book im using thus far.

    I need to be able to do the delete on _id and what not (or just a delete *this) so im not filling up the heap



    halp me koob
    Last edited by PrivateParts; Mar 29th, 2009 at 05:57 AM.

  2. #2
    Seņor Member Reputation: 393
    Join Date
    Feb 2008
    Location
    Loco's Biatch
    Posts
    2,262

    Default Re: c++ halp

    First of all, your mString constructor should be:

    Code:
    if( _ptr != NULL )
    {
    delete [] _ptr;
    _ptr = NULL;
    }
    You might get some nasty unpredictable effects if you try to delete a NULL pointer, AFAIK.
    Also make sure your _ptr gets set to NULL in your constructor otherwise it will get a gibberish value and, when deleted, cause memory corruption.

    Now onto your main problem - you gotta learn to read your error messages PP!

    delete [] _id;
    Error 1 error C2440: 'delete' : cannot convert from 'myString' to 'void *'

    What is the error telling you? It expects a pointer (void *) but you are feeding it a... NOT pointer. You're feeding it a myString member variable which has NOT been allocated dynamically. Hence, you cannot call delete on it.

    So what do you do in this case?























    Nothing.


    You see, your CProduct object has a myString _id; NOT myString* _id. Your _id is not a pointer, but a statically allocated variable that is created whenever a CProduct object is created. When CProduct is destroyed, it will destroy all of its members, which willl automatically call their destructors - including your _id and other myStrings.

    That is part of the reason to use OOP and encapsulation - once you get your myString class working, you don't have to worry about what it does on the inside, it should be able to take care of itself!

    I think you were confused with when a destructor gets called, thinking you need to explicitly called a "delete," which is not entirely true.

    A destructor gets called whenever an object is destroyed.

    when is an object destroyed? there are few, pretty logical ways, depending on how it is created, for instance:
    * If it is statically allocated inside a function, then it will get automatically destroyed when the function ends.
    * If it is dynamically allocated via new, it will get destroyed when you manually call delete on its pointer
    * If it is a member of another class, it will get automatically destroyed whenever the object of that class gets destroyed (for the same reasons as above). <-- this is your scenario here with CProuduct and its member _id!

    Basic rule of thumb: If you're not creating an object view "new", never, EVER call delete on it. It will get destryoed automatically at a logical point (outlined above).

    In your case, your myString will clean itself up in the destructor, so you don't have to worry about it. Your CProduct, which has myString as member vars (not dynamically allocated), will automatically destroy them when it itself is destroyed, so you're good. All you have to worry about is making sure a CProduct object gets properly destroyed (i.e. call delete if you called new), and everything will be taken care of.

    So, in a nutshell, here is what would happen if you destroyed your CProduct object

    * CProduct is being destroyed
    * CProduct calls its destructor
    --> CProduct now automatically destroys all of its member vars
    ---> _id is being destroyed
    ----> _id calls its destructor, deleting its _ptr, cleaning up memory
    ---> _name is being destroyed
    ----> _name calls its destructor, deleting its _ptr, cleaning up memory

    For more info, check out this: http://www.parashift.com/c++-faq-lite/dtors.html

    or just a delete *this
    DONT do this (also it should be delete this not delete * this). If your object is not created via new it will explode. also, after you call it, the object becomes invalid so if you do it inside its own function and the function tries to access its member vars, boom. Also a destructor is called when you do a delete on a pointer, is if you put "delete this" in a destructor - infinite loop.
    Last edited by Koobazaur; Mar 29th, 2009 at 11:55 AM.

  3. #3
    БЯōҚĔй Reputation: 1679 Face's Avatar
    Join Date
    Feb 2008
    Location
    Nowhere
    Posts
    4,999

    Default Re: c++ halp

    10 INPUT "What is your name: ", U$
    20 PRINT "Hello "; U$
    30 INPUT "How many stars do you want: ", N
    40 S$ = ""
    50 FOR I = 1 TO N
    60 S$ = S$ + "*"
    70 NEXT I
    80 PRINT S$
    90 INPUT "Do you want more stars? ", A$
    100 IF LEN(A$) = 0 THEN GOTO 90
    110 A$ = LEFT$(A$, 1)
    120 IF A$ = "Y" OR A$ = "y" THEN GOTO 30
    130 PRINT "Goodbye ";U$
    140 END

  4. #4
    The Original Reputation: 1538 PrivateParts's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    4,814

    Default Re: c++ halp

    With myString class i had put a cout in the destructor to see when it was destroying objects.

    If i do something like obj1 = "Testing" (uses a one param constructor that has a char array as its argument) and then do obj1 = "Testing 2" , it does a destroy.

    What i was getting thrown off by is if i change the values in an object for CProduct in the same way it wasnt spitting out this cout message.. forgot i commented out that cout so thought it wasnt destroying when i would reintalize something.
    Thought they werent getting destroyed so the heap was filling up


    One more thing though. In my default constructor for CProducts i figured i would set _id and what not to NULL. Though it crashes if i do. This because _id isnt a pointer? The book (what ive read atleast) hasnt really talked about NULL.
    Last edited by PrivateParts; Mar 29th, 2009 at 06:03 PM.

  5. #5
    Seņor Member Reputation: 393
    Join Date
    Feb 2008
    Location
    Loco's Biatch
    Posts
    2,262

    Default Re: c++ halp

    Correct. NULL is reserved for pointers.

    Or rather, should be. In reality, NULL is not a built in C++ type but usually a define somewhere (for instance in windows.h if you're usign that). Most likely it is simply set to 0. Meaning, you can set it to an integer. But you shouldn't since it will be confusing for other people who read your code.

    (Also, FYI, setting a pointer to NULL or 0 will usually have the same effec, since NULL is most likely 0. You should stick to using NULL instead of 0, though, since it may in fact be platform dependant, and different platforms may define NULL differently).

    If you want to nullify a string, simply give it a function like Empty() which empties it out and another IsEmpty() which returns true if the string is empty (i.e. length == 0 or _ptr == NULL ).

    Also I would advise against your CProduct setting _id to NULL i.e. "empty" Instead have the myStrings set themselves to be empty when they are created. Again going back to what I was saying earlier - the objects should take care of themselves as much as they can. This way you don't have to worry about low level stuff when you're coding high level stuff.
    Last edited by Koobazaur; Mar 30th, 2009 at 02:56 AM.

  6. #6
    The Original Reputation: 1538 PrivateParts's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    4,814

    Default Re: c++ halp

    Thanks

    Another question though.

    I pretty much have my two classes implemented. I can load up a wack of product objects from a txt file

    What i need to do now is create 2 arrays of pointers which will point to the elements of a Cproduct array

    "Application will manage the order of the array of products by using 2 arrays of pointers that will be pointing to elements in the array of products"

    "There should be 2 arrays of pointers, one so that the array is in id order, the second so the array is in product name order.

    Code a sort function that will first set the array of pointers to point to each element in the array of objects (the address of each of the elements), then this function will sort the 2 arrays of pointers (note that the array of products does not change only the pointers are swapped) "


    ie:
    CProduct products[10]; //my product array (works fine. i can load data into it from file)

    typedef CProducts* IntArrayPtr;
    IntArrayPtr *pToProduct = new IntArrayPtr[10]; //one of them

    I can make pToProduct point to products by: pToProduct[0] = &products[0]; (though would use a loop to do all the pointing)

    This question isnt demonstrated in the book or my instructors powerpoints so i have absolutely no idea how i get the _id only out of pToProduct so i can do some sort of comparison for a sort...

    or would i really need to get at id/name through the array of pointers. suppose since they are being set in the same order as the array of products from the get go i could try doing my comparison with my products accessor functions and sort the arrays of pointers based off that.
    Last edited by PrivateParts; Mar 30th, 2009 at 04:02 AM.

  7. #7
    Seņor Member Reputation: 393
    Join Date
    Feb 2008
    Location
    Loco's Biatch
    Posts
    2,262

    Default Re: c++ halp

    Quote Originally Posted by PrivateParts View Post
    typedef CProducts* IntArrayPtr;
    IntArrayPtr *pToProduct = new IntArrayPtr[10]; //one of them
    Why not just do this:

    Code:
    CProduct* pToProduct[10];
    since your product array is always 10 anyway


    As for comparing, if your CProduct's _id is public you can just compare it directly. If it's not just give it something like:
    Code:
    //note this returns const reference - important for efficiency!
    const myString& GetID() const
    {
     return _id;
    }
    And use that in your sort like this:


    Code:
    //referencing the pointer to the product using ->, 'x' is a random element you want to get
    pToProduct[x]->_id; //only if _id is public, OR:
    pToProduct[x]->GetID(); //probably much safer

    Now you have all you need to do the sorting. If you are required to fill out your array of pointers first, bubble sort sounds like a good choice here. Google it, it's pretty simple.

    Also, there are a few sort functions in the standard c++ library, but I am guessing your prof / book does not want you using them here.

    EDIT: btw, "pToProduct" is a horribly confusing and misleading name. I'd call it "productPointersSortedByID" or something like that.

    EDIT: also "IntArrayPtr" is bad too, sounds like the type is a pointer to an array of integers. I'd call it "CProductPtr." Or just skip doing the typedef altogether, simply " CProduct* " is clear enough.
    Last edited by Koobazaur; Mar 30th, 2009 at 06:13 AM.

  8. #8
    The Original Reputation: 1538 PrivateParts's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    4,814

    Default Re: c++ halp

    Quote Originally Posted by Koobazaur View Post
    Why not just do this:

    Code:
    CProduct* pToProduct[10];
    since your product array is always 10 anyway


    As for comparing, if your CProduct's _id is public you can just compare it directly. If it's not just give it something like:
    Code:
    //note this returns const reference - important for efficiency!
    const myString& GetID() const
    {
     return _id;
    }
    And use that in your sort like this:


    Code:
    //referencing the pointer to the product using ->, 'x' is a random element you want to get
    pToProduct[x]->_id; //only if _id is public, OR:
    pToProduct[x]->GetID(); //probably much safer

    Now you have all you need to do the sorting. If you are required to fill out your array of pointers first, bubble sort sounds like a good choice here. Google it, it's pretty simple.

    Also, there are a few sort functions in the standard c++ library, but I am guessing your prof / book does not want you using them here.

    EDIT: btw, "pToProduct" is a horribly confusing and misleading name. I'd call it "productPointersSortedByID" or something like that.

    EDIT: also "IntArrayPtr" is bad too, sounds like the type is a pointer to an array of integers. I'd call it "CProductPtr." Or just skip doing the typedef altogether, simply " CProduct* " is clear enough.

    Ah yea, i didnt have my gets as const reference. I tried using pToProduct[x]->getID() before but wasnt getting anywhere. changed it and its working now.

    Also the naming conventions here, i was copy and pasting a bit from my instructors power points. So yea, intarrayprt isnt good.

    I used CProduct *pointToID[10] and *pointToName[10]

    We need to code our own sort. I will work on that today and hope it goes smoothly..

  9. #9
    Seņor Member Reputation: 393
    Join Date
    Feb 2008
    Location
    Loco's Biatch
    Posts
    2,262

    Default Re: c++ halp

    Quote Originally Posted by PrivateParts View Post
    Also the naming conventions here, i was copy and pasting a bit from my instructors power points. So yea, intarrayprt isnt good.
    WTF what is wrong with your instructor? I really want to ask him what the hell he was thinking naming it that...

    We need to code our own sort. I will work on that today and hope it goes smoothly..
    Like I said, most sorts are actually really simple in principle once you understand how they work. Google is your friend here.

  10. #10
    The Original Reputation: 1538 PrivateParts's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    4,814

    Default Re: c++ halp

    My instructors notes have always been horrible. Has made it a pain in the ass trying to learn from her in VB and C++.

    Notes off the blackboard are horrible too. She'll start writing something in one spot in a certain color, draw some boxes and lines between them to show memory locations, start writing in a different color elsewhere on the board and so on. Makes it hard to make understandable notes.

    Anyway, ive coded a few sorts before so i should be able to get it done

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. vista halp ?
    By TylerDurden in forum Random Nonsense
    Replies: 13
    Last Post: Nov 11th, 2008, 10:21 PM
  2. Halp! Dissertation stuffs.
    By Zirconium Blade in forum Random Nonsense
    Replies: 17
    Last Post: Jul 26th, 2008, 06:39 AM
  3. I broke my pants, tmh halp ;_;
    By Koobazaur in forum Random Nonsense
    Replies: 17
    Last Post: Jul 13th, 2008, 07:47 PM
  4. Halp halp this is urgent!
    By SimonS in forum Random Nonsense
    Replies: 9
    Last Post: Mar 21st, 2008, 01:50 PM
  5. Halp BETA Test my uni Project
    By SimonS in forum Random Nonsense
    Replies: 9
    Last Post: Feb 20th, 2008, 05:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

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