Thread: Queues, Nodes and passing instances of a class

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Unhappy Queues, Nodes and passing instances of a class

    I've got a question concering how to pass instances of a class through nodes/queues.

    My question deals with arrays. When I push an instance of class on to my queue and then proceed to pop that same object off of my queue and call the function to return this instance of classes array of values, I can index the first value (ie array[0]) and recieve the first value fine. When I try to index any other value (array[1] for instance) I get these funny numbers like -10958358 .

    Now I did not instatiate this classes array like this, why does this happen?

    If source code is needed, I can post, but it is quite lengthy.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think source code is needed for this. If possible, cut it down to only what is directly applicable.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What Zach asked for would definitely not hurt. But I think I understand what you are asking enough to help. It sounds to me like you are not allocating new data in your queue. And specifically show the lines of code where you add data and where you try to access data by an index.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Allright, here is my code.

    My customer/data class (basically the class that is encapsulated by the node)
    Code:
    Customer :: Customer (int id, int items, int seconds)
    {
    	customerValues[0] = id;
    	customerValues[1] = items;
    	customerValues[2] = seconds;
    	customerValues[3] = items;
    	}
    
    int* Customer :: getCustomerValues()
    {
    	return customerValues;
    }

    My Node class
    Code:
    Node :: Node ()
    {
    	link = NULL;
    }
    
    Customer* Node :: getCustomer ()
    {	
    		return current;
    }
    
    void Node :: setNextLink (Node* next)
    {
    	link = next;
    }
    
    void Node :: setCustomer(Customer* incoming)
    {
    	current = incoming;
    }
    
    Node* Node :: getLink()
    {
    	return link;	
    }
    My queue (now, my pop function doesn't really pop it off yet, I want to return the correct values from my customer before I start popping off values, you'll see what I mean...)

    Code:
    Queue :: Queue (string type)
    {
    	queueSize = 0;
    	frontNode = new Node;
    	backNode = frontNode;
    	frontNode->setNextLink(NULL);
    	queueType = type;
    }
    		
    bool Queue :: isEmpty()
    {
    	return frontNode == backNode;
    }
    
    void Queue :: push(Customer incoming)
    {
    		backNode->setCustomer(&incoming);
    		backNode->setNextLink(new Node);
    		backNode=backNode->getLink();
    		backNode->setNextLink(NULL);
    		queueSize++;
    
    }
    
    Customer Queue :: pop()
    {
    //	Node *nodeHolder;
    	
    	if (isEmpty())
    		cout << "Queue is Empty!";
    	else
    	{	
    		Customer frontCustomer = *frontNode->getCustomer();
    cout << frontCustomer.getCustomerValues()[0];// if I index it with zero, I get the correct value
    		cout << (frontCustomer.getCustomerValues()[2]); //now if I try doing this, I get funny values
    		return frontCustomer;
    		/*nodeHolder = frontNode;
    		frontNode = frontNode->getLink();
    		delete nodeHolder;
    		queueSize--;
    		*/	
    	}
    
    	
    	
    }
    
    int Queue :: size()
    {
    	return queueSize;
    }
    and finnally my driver for it, just for testing, it doesn't really do much yet

    Code:
    
    main()
    {
    	string y = "express";
    	Customer one (100,100,100);
    	Queue lane(y);
    	lane.push(one);
    	 lane.pop();
    
    
    	
    	
    	
    }
    I think my problem deals with how I'm referencing my array, I dunno.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Show the declaration of customerValues in Customer.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Also show the declaration of Node, to see how you represent Customer in Node.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Hmmm something is not within the code that you are showing. If it isn't a big deal just upload the cpp file.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Everything is within the code above. I did not include the header files because I thought that may be redundant. The declaration of customerValues is in the header and I declare it as an array: int customerValues[4] .

    I represent Customer in node by having a pointer point to the incoming customer reference. I return that pointer with Node.getCustomer();

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Quote Originally Posted by xshapirox
    Allright, here is my code.
    Code:
    void Queue :: push(Customer incoming)
    {
    		backNode->setCustomer(&incoming);
    		backNode->setNextLink(new Node);
    		backNode=backNode->getLink();
    		backNode->setNextLink(NULL);
    		queueSize++;
    
    }
    incoming is a local copy. When push returns the new Node has a pointer to an object that has had it's destructor called and has gone out of scope, hilarity insues.

    The normal way to avoid this is to have node contain a Customer, rather than a pointer to a Customer. There is no need for backNode to be constructed without a customer. We can also simplify things by removing a lot of your getLink() functions and just making Queue a friend of Node. Remember the main reason we want to keep as much private as possible is to avoid having to "chase" our changes. As Queue is closely related to Node it makes sense to have to change Queue extensively if we change Node, but we don't want anyone else to have to know about the details.

    Code:
    class Node {
        Customer cust;  // <= copy
        Node *next;
        Node(const Customer &c) : cust(c), next(0) {}
    //                             copy ctor ^^^^  
        friend class Queue;
    };
    ...
    void Queue::Push(const Customer &c) { // <= not a copy
        if(tail) {
            tail->next = new Node(c); // <= copy made
            tail = tail->next;
        }  else { // empty Queue
            head = new Node(c); // <= copy made
            tail = head;
        }
    }
    // Access, undefined if empty.
    Customer & Queue::Top() const {return head->cust;}
    ... //Useage
    Queue q;
    q.Push(Customer(0,1,3));
    Customer c = q.Top();
    q.Push(c);
    q.Push(q.Top());

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Allright, I have a little problem though:

    When I try to declare a Customer in my Node header file, I get this error:

    Code:
    In method `Node::Node(const Customer &)':
    Node.cpp:5: no matching function for call to `Customer::Customer ()'
    Customer.h:9: candidates are: Customer::Customer(int, int, int)
    Customer.h:14:                 Customer::Customer(const Customer &)
    I don't know why though...

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You have to use the initialization list like grib showed you because your Customer class doesn't have any constructors that take no arguments. You want to copy construct the Customer member of the Node in the initialization list.
    Code:
        Node(const Customer &c) : cust(c), next(0) {}
    //                    copy ctor ^^^^  

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    When I try the above and try to compile I get this error:

    Code:
    Node.cpp: In method `Node::Node(const Customer &)':
    Node.cpp:4: type `Customer' is not an immediate basetype for `Node'
    This is in my Node source file, not the header.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Thanks, I think I got it. Forget the above message.

    I understand now.

    I really appreciate ya'lls help. If there was anything I could do to show my appreciation I would (I got a bottle of brandy and a bag of grass, but I doubt anyone lives near me)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with C++ linked list using objects
    By pityocamptes in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 11:12 AM