Thread: I'm back baby (With a new set of problems)

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

    Unhappy I'm back baby (With a new set of problems)

    First of all thanx to every1 who helped me in my last prob. There were some good tips and to those I listened to I changed the prog. Anyway this is a new prob.
    Pls I know some ppl are against me using C-strings but I really I'm adopting this style of programming to try and learn the workings of C++ and make them it sink in hence the difficult way. Also it's my first OOP program. Anyway here goes:

    The Class:
    Code:
    class dbase
    {
    static int populus;
    	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   <======= This is new
    	void set_details(char *nm, int ag, int telph);
    	void show_details();
    
    	//operator declarations
    	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);
    	friend bool searchAndDisplay(dbase &list, char *name);
    	friend int countEntriesInList(dbase &list);
    	friend bool deleteFromList(dbase &list, char *name, int number);
    };  //end of class
    Where I think the problem stems from though it seems to be working fine:
    Code:
    dbase dbase::operator=(dbase *op2){
    	name = new char[(strlen(op2->name)+1)];
    	strcpy(name, op2->name);
    	age = op2->age;
    	phone = op2->phone;
    	link = op2->link;
    return *this;
    }
    The function that uses it:
    Code:
    bool deleteFromList(dbase &list, char *name, int number)
    {
    dbase *iterator, *key;
    
    iterator = &list;
    
    if ( (strcmp(iterator->name, name) == 0) && (iterator->phone == number) )
    { list = list.link; return true; }
    else
    {
    	while((iterator->link != NULL) && ((strcmp(iterator->link->name, name) !=0) && (iterator->link->phone != number)) )
            iterator = iterator->link;
    
            if( (strcmp(iterator->link->name, name) ==0 ) && (iterator->link->phone == number))
    		{
    			key = iterator->link;
    			iterator->link = key->link;
    			key->link = NULL;
    			key->~dbase() ;
    			return true;
    		}
    
    }// end of 1st else
    return false;
    }
    Main itself:
    Code:
    int main()
    {
    dbase list; //global declaration
    
    initialiseList(list);
    if (listIsEmpty(list) == true)
    cout<<"List is Truly empty.\n\n";
    
    getchar() ;
    insertIntoList(list, "Bobo",2, 345);
    insertIntoList(list, "Cat",3, 587);
    insertIntoList(list, "Adt",5, 757);
    insertIntoList(list, "Doc",8, 434);
    insertIntoList(list, "Cot",4, 328);
    displayList(list);
    
    if (deleteFromList(list, "Adt", 757) == false) {cout<<"\n>>>>Unable to delete.<<<<\n";}
    
    cout<<countEntriesInList(list)<<" is the number of objects that currently exist.\n";
    displayList(list);
    
    return 0;
    }
    The problem is that when I call display list for the second time there seems to be only 1 object left as it seems the rest have been deleted. Now this only happens when I try to delete from the top of the list. I know the way I'm deleting from the top is odd, but this is due to my last problem which I found is due to the fact I was trying to change what the reference variable was pointing to as opposed to it's contents.
    Shout out to cyberCloWn. This is mah 1st program in C++ and I'm also Using a Herbert Schildt's book, pretty damn good eh? (Mine's called "The Complete C++ Reference")
    Again sorry for the longs posts.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    friend void initialiseList(dbase &list);
    friend void displayList(dbase &list);
    friend void insertIntoList(dbase &list, char *name, int age, int number);
    Need to post these too.

    gg

  3. #3
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I'm posting the whole code:

    Thanx

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    ...
    destroying: Bobo.
    destroying: Cat.
    destroying: Cot.
    destroying: Doc.
    1 is the number of objects that currently exist.
    <seg fault>
    The first question you should be asking youself is "why are all these destrcutors being called when I call deleteFromList()?"
    Originally posted by Codeplug

    >> bool operator==(dbase &op2);
    >> dbase& operator=(dbase &op2);
    Use references for these.
    Remember that suggestion? Implement it and post back why that fixes the problem (or if you need help determining the why).

    gg

  5. #5
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I implemented those changes codeplug and it refuses to recognise this line as valid outputting the following error:
    Code:
     
    list = list.link;
    
    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class dbase *' (or there is no acceptable conversion)
    As to why it was calling the destructors in the delete function I am , for the want of a better word, BAFFLED. Would you please enlighten me as to why, because truly this only happens whe I delete from the top of the list and since I use the = constructor I created I was expecting the first two elements to be the same. seeing as there hasn't been any attempt to call the destructors unless it enters the else condition. But it shouldn't if I'm deleting from the top. right?

    Stupid me. Solved it.
    Last edited by WDT; 12-08-2003 at 12:17 PM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    error C2679: binary '=' : no operator...
    You probably didn't make the change in both the friend declaration and the implementation.
    As to why it was calling the destructors...BAFFLED
    Code:
    if ((strcmp(iterator->name, name) == 0) && (iterator->phone == number))
    { 
        list = list.link; //<---- destructors being called here
        return true; 
    }
    Think in terms of: "Why does operator=() need to return a reference?

    gg

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    The smaller problem is that you're mixing pass-by-pointer with pass-by-value with pass-by-reference without really understanding how each works.

    Specifically, your implementation of your assignment operator is not quite right:
    Code:
    dbase& dbase::operator=(dbase& op2);
    Look at the changes that have been made in red. Your assignment operator should receive an object by reference and return this by reference.


    However...

    Since you are trying to learn C++ "the hard way", I'll let you know that the larger problem here lies in your design. You are trying to use 1 class for 2 purposes: (1) your data record, and (2) your list container.

    Ideally, these should be abstracted into separate classes and this would make your program MUCH easier to debug.

    This is the main reason why your assignment operator is causing you problems. If your class represents 2 distinct ideas, which should the assignment op copy? The entire list or just the data node? What about the copy constructor? Your functions and objects can't have any well-defined meaning.

    I'd suggest splitting this up into 2 classes and trying to make it work that way:
    Code:
    class dbase_node
    {
    public:
      // constructor, accessor, mutator functions
    public:
      friend class dbase; // allow dbase access to private members
    private:
      char *name;
      int age;
      int phone; 
      dbase_node *prev;  // 2 links for 
      dbase_node *next;  // doubly linked list
    };
    
    class dbase
    {
    public:
      // constructor, accessor, mutator functions
      // all of your functions that are currently "friend" functions
      // should be implemented here as member functions
    private:
      unsigned int count;  // number of nodes in list
      dbase_node* head; // first and
      dbase_node* tail;    // last nodes in list
    };
    I hope that provides some help. Give it a try and post back if you have any problems.


    -tf

  8. #8
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ok thanx guys.
    1st Codeplug:
    I made the changes to both declarations but I found something which fixed the problem. Ad hoc, Yes I know, but it gives me the correct output.
    Code:
    if ((strcmp(iterator->name, name) == 0) && (iterator->phone == number))
    { 
        list = list.link; //<---- destructors being called here
        return true; 
    }
    first of all the line you've pointed out above. i need you to explain a lil' because this is where I fixed the prob. I thought that would make use of mah = operator and copy node 2 to 1 hence the 1st two would be duplicates then I can then go ahead and link newly reformed node 1 with node 3. (confuse ya? coz that'll baffle me too) second that part has now been reformed to this:
    Code:
    if ( (strcmp(iterator->name, name) == 0) && (iterator->phone == number) )
    { 
    iterator = list.link;
    list = *list.link;     <<<<<=== Would be lying if ah said ah knew 100% what's happening but I know 50% so 's good. NO?
    list.link = iterator->link;
    iterator->link = NULL;
    iterator->~dbase(); 
    return true; 
    }
    And it works fine. at least give the right output. All because of the line you pointed out.

    2nd Froggy:
    actually I understand how pass by value works very well which is why I was passing everything by reference or trying to at least. 's just that I get the other two mixed up when it comes to assignment and you're half right, I almost don't understand the difference between by pointer and by reference at least let's say I can't seem to work with them interchangably which is why I'm jumping in the deep end this way. Passing by value would have been way much easier than this and probably wouldn't result in all my problems but I wanted to do it this way and avoid making copies of entire list(s). Also implemented those changes.
    Another thing I like the way you redid my code, it's great 'cept that for me 's a little complicated. I just wanted one container for both coz it's a whole lot easier to picture in mah head and also helps when imagining the list state(unfortunately this doesn't coincide with the way C++ works)
    And again Thanx to the both of you for taking your time to leaf through mah mess. I probably wouldn't have.
    Last edited by WDT; 12-08-2003 at 07:47 PM.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I think the absolute best next step for you WDT, is to learn how to use your debugger.
    I'm not trying to be funny or anything - I can tell that you genuinely want to learn.
    From what I can tell of what you already know, you have learned far too much to still not know how to use a debugger.

    If I've assumed too much, then set a breakpoint on the "<---" line and "step-into" until destructors start getting called.

    [I can help with VC++ 6.0 or gdb (debugger for gcc and g++), if that's what you use.]

    gg

  10. #10
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    I've been found out. Guilty as charged. 'm I that obvious?

    I normally program C/C++ in the Unix (freeBSD) environ but in mah yrs of prgramming I've never made an attempt to get to know how to use the debugger for any language I'm learning. To tell you the truth I never planned to. Anyway thanx a Lot. I would be grateful for any help in both.
    I actually started to use MS VC++ coz Mah Unix went down so I''m new to this environ. But I won't lie, it makes coding in principle faster but practical coding a head job (i.e. windows/openGL) a little confusing with primitives like DWORD and all that MS crap.
    Anyway I hope not to burden you with my lack of inexperience for much long as I learn pretty quick that's if you're willing.
    Thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  3. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  4. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM