Thread: reference object

  1. #1
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Question reference object

    Code:
    void insertIntoList(dbase &list, char *name, int age, int number)
    the problem is I'm trying to insert to the top of the list but I keep getting an error which I'm guessing results from a destructor call to tmp. (note the passing of list as a reference variable)
    This is where I'm guessing the error comes up:
    Code:
    if (*iterator->name > *name){
    tmp = new dbase(name, age, number);
    tmp->link = &list;
    list = &tmp;
    tmp = NULL; 
    }
    iterator and tmp are declared thus: dbase *iterator, *tmp;
    but I'm guessing because tmp is now nullified when the destructor's called so there's nothing to destroy though it realises that the object was created earlier.
    Is there a solution to my problem or should I not pass the list as a reference variable and just let the function return the object?
    Thank you all for your time

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    The code you post is hard to understand because there are no declaration for some of the variables. Post the entire function.

    Kuphryn

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Code:
    void insertIntoList(dbase &list, char *name, int age, int number){
    dbase *iterator, *tmp;
    
    iterator = &list;
    	
    if (listIsEmpty(list)) {list.set_details(name, age, number);}
    else{
        while ((iterator->link != NULL) && (*iterator->name < *name)) 
         iterator = iterator->link;
    
    if((iterator->name < name) && (iterator->link == NULL))
       { iterator->link = new dbase(name, age, number); }
    else{
        if (*iterator->name > *name)
        {tmp = new dbase(name, age, number);
          tmp->link = &list;
          list = &tmp;
          tmp = NULL;
        }
        else{    }// end of 3rd else
    }//end of 2nd else
    }// end 1st else
    }
    here's the whole function and thanx.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't you think knowing the dbase type might be pretty important to someone wishing to help?

    gg

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Sorry. I'm new to both forum posting and C++.
    Code:
    class dbase{
    	char *name;
    	int age;
    	int phone;
    	dbase *link;
    public:
    	dbase() {};	// constructor
    	dbase(char *nm, int ag, int telph);
    	~dbase();
    	dbase(const dbase &x); //copy constructor
    	void set_details(char *nm, int ag, int telph);
    	void show_details();
    
    	//operator definitions
    	bool operator==(dbase op2);
    	//dbase operator=(dbase op2);
    
    friend void initialiseList(dbase &list);
    friend bool listIsEmpty(dbase &list);
    friend void displayList(dbase &list);
    friend void insertIntoList(dbase &list, char *name, int age, int number);
    };  //end of class
    I've just added the copy constructor to see if I can get round my problem.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Interesting.

    Try adding parentheses.

    if (*(iterator->name) > *name)
    ...

    Kuphryn

  7. #7
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    That's not the problem Kuphryn. My conditionals works fine. In fact if you remove & from this: [B]list = &tmp;[B] The whole thing compiles but you'll get a run time error which like I originally suspect is to do with the destructor being called when the function exits.
    I need a work-around around this problem because the tmp pointer is being nullified before this(i.e. function exit) and the destructor frees up the allocated memory for the char *name member.
    If anyone wants the whole code please say so as it's commented to cater for quick reading. Or I could post it. (I just hate posting long codes.)
    I'm trying to work around this problem by creating a copy operator.
    My apologies if I sound rude or impatient. 's not intended.

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    At:
    Code:
    tmp = new dbase(name, age, number);
    tmp->link = &list;
    list = &tmp;
    tmp receives a new object, and then list receives this new object. Is this what you really want?
    Nothing more to tell about me...
    Happy day =)

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I think that I understood what you want to do, it is a ordered list, correct? But you are comparing char arrays with < and > operator. I am not sure if you know what you are doing with this, sorry if this is really your intent. But you must use strcmp to compare two words, or you can use the string type, which provides <, = and > operators to compare.
    Nothing more to tell about me...
    Happy day =)

  10. #10
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Yes Gustavossrra. I want an ordered list. but everything works fine even the comparison works. But when you try to insert at the top of the list. It gives a runtime error. Change the & to * At:
    Code:
    list = &tmp;
    .

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    In your code, tmp is a pointer and list is a reference.

    // This code is incorrect.
    list = &tmp;

    // Correct
    list = *tmp;

    Kuphryn

  12. #12
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I know. Error on my part, and I pointed it out in my last post. But my problem still exists. Do you think I should just change the function so that it doesn't take a reference variable as a parameter but rather it returns the newly modified list or is there a work-around to my problem?

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yes Gustavossrra. I want an ordered list. but everything works fine even the comparison works.
    Code:
    if (*iterator->name > *name)
    You do realize that the above code only compares the ASCII value of the first character. That means that "A" will be greater than "b" and comparing names like "Andy" and "Adam" will be non-deterministic.

    There are probably multiple design and coding issues that need to be dealt with, but here's the problem with what you posted.
    Code:
    tmp = new dbase(name, age, number);
    // prepend this new object by assigning "link" to the address of "list"
    tmp->link = &list;
    // make tmp the new head list object by assigning all members of tmp to list
    // including the link member, which equals &list
    // you now have a one element circular linked list
    list = *tmp;
    gg

  14. #14
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions. There are several logic errors in your code. Essentially, you have two choices: reference and pointer.

    Kuphryn

  15. #15
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I'm aware of the conditional checking and have corrected this ages ago. I'm just using the posted version to show the logical construct of the function. It shouldn't create a circular list as I nullify the tmp pointer after the (( list = *tmp; ) this is shown from my 2nd post) assignment. Anyway as it stands this is the code right now in it's current form:
    Code:
    void insertIntoList(dbase &list, char *name, int age, int number){
    dbase *iterator, *tmp;
    
    iterator = &list;
    	
    if (listIsEmpty(list)) {list.set_details(name, age, number);}
    else{
            while ((iterator->link != NULL) && (iterator->name < name)) 
            iterator = iterator->link;
    
            if ((iterator->name < name) && (iterator->link == NULL))
           { iterator->link = new dbase(name, age, number); }
            else{
    	if (*iterator->name > *name){
    	tmp = new dbase(name, age, number);
    	tmp->link = &list;
    	list = *tmp;
    	tmp = NULL;
    	}
    	else{}// end of 3rd else
           }//end of 2nd else
    }// end 1st else
    
    }
    Hopefully this should clear things up.
    The problem still exists which I suspect is still down to my suggested destructor problem. If it is please advise if there is a work-around and kuphryn I'm not sure what you mean by your last post. Do you mean to pass list as a pointer or for the function to return a reference/pointer variable?
    Anyway my current solutions are maybe to overload an operator or define a copy constructor. Or more easily return the modified tree.
    Last edited by WDT; 12-02-2003 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to maintain and return local object reference?
    By benshi in forum C++ Programming
    Replies: 14
    Last Post: 12-16-2007, 02:17 PM
  2. Managing a reference object
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 11-23-2006, 07:41 PM
  3. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  5. Beginners guide to OOP from a Java background?!
    By eggsy84 in forum C++ Programming
    Replies: 6
    Last Post: 06-08-2005, 06:13 AM