8 Replies - 981 Views - Last Post: 22 March 2013 - 06:00 PM Rate Topic: -----

#1 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Linked Queue Display function error cannot convert...

Posted 21 March 2013 - 06:42 PM

I'm trying to write a queue simulation program and I decided to use a linked list queue and templates. The queue is a line of customers which are going to be represented by integers. I'm getting this error from the display function in Queue.cpp file:
error cannot convert 'Customer' to 'NodeType<Customer>*' in assignment:
Here is the display function from queue.cpp
template<class C>
void Queue<C>::display(ostream& disp) const
{
	NodeType<C>* location = front;                                   // Sets location to first item in queue

	while(location != NULL)
	{
		location = location->info;
		disp << location << endl;
		location = location->next;
	}



Here is driver.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Customer.h"
#include "Queue.h"

using namespace std;

int main()
{
	Customer c;
	Queue<Customer> line;
	int simLngth;
	int avgTrans;
	int numServers;
	int avgArvl;
	char answer;
	int count = 1;
       
        // Get simulation parameters
	cout << "Please enter values for the following parameters: " << endl;
	cout << "Simulation length: ";
	cin >> simLngth;
	cout << "Average transaction time: ";
	cin >> avgTrans;
	cout << "Number of servers: ";
	cin >> numServers;
	cout << "Average time between arrivals: ";
	cin >> avgArvl;

	for (int minutes = 0; minutes <= simLngth; minutes++)
	{
		if (minutes % avgArvl == 0)
		{
			c = Customer(count);
			line.enqueue(c);
			count++;
		}
	}
        cout << "Queue contains: " << endl;
	line.display(cout)
	
	
	return 0;
}



and Customer class just has a default constructor and a constructor Customer(int newCustomer) which sets the private data member int customer to newCustomer. Thanks in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Linked Queue Display function error cannot convert...

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3566
  • View blog
  • Posts: 11,079
  • Joined: 05-May 12

Re: Linked Queue Display function error cannot convert...

Posted 21 March 2013 - 06:57 PM

Is the error on this line?
        location = location->info;


If so, why are you trying to assign the node value to a node pointer? What are you trying to achieve?

This post has been edited by Skydiver: 21 March 2013 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Linked Queue Display function error cannot convert...

Posted 21 March 2013 - 06:57 PM

location is a node pointer and location->info is most likely a Customer type. You probably just want to print the Customer data location->info.

08        location = location->info;
09        disp << location << endl;


Was This Post Helpful? 1
  • +
  • -

#4 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 10:33 AM

Ok I've changed it to this:
template<class C>
void Queue<C>::display(ostream& disp) const
{
	NodeType<C>* location = front;

	if (!isEmpty())
	{
		while(location != NULL)
		{
			disp << location->info << endl;  // error here
			location = location->next;
		}
	}
	else 
		disp << "This queue is empty" << endl;



But now I'm getting this error:
error: no match for 'operator<<' in 'disp << location->NodeType<Customer>::info'
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 11:08 AM

You don't appear to have an overloaded function operator<< that prints a Customer to the stream. How do you plan to display the Customer data?
Was This Post Helpful? 1
  • +
  • -

#6 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 03:18 PM

Okay so I fixed the problem and the program compiles and runs but whenever I try to print the items in the queue it only prints the last item added and nothing else. The length of the queue is right but it's not printing out all the elements. Here is the display function by itself, the entire Queue.cpp, and Driver.cpp:

display function:
template<class C>
void Queue<C>::display(ostream& disp) const
{
	NodeType<C>* location = front;
        while(location != NULL)
        {
            disp << location->info << endl;
            location = location->next;
        }
}



Queue.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>


using namespace std;

template<class C>
struct NodeType
{
	C info;
	NodeType<C>* next;
};

template<class C>
Queue<C>::Queue()
// Function: Default constructor for Queue class
// Pre: None
// Post: front and rear are set to NULL
{
	front = NULL;
	rear = NULL;
	length = 0;
}

template<class C>
int Queue<C>::getLength()
{
	return length;
}

template<class C>
void Queue<C>::MakeEmpty()
{
	NodeType<C>* tempPtr;

	while (front != NULL)
	{
		tempPtr = front;
		front = front->next;
		delete tempPtr;
	}
	rear = NULL;
}

template<class C>
Queue<C>::~Queue()
// Function: Class destructor
// Pre: Queue is not empty
// Post: Queue is empty and all elements have been deallocated
{
	MakeEmpty();
}

template<class C>
bool Queue<C>::isFull() const
// Function: Determines if queue is full
// Pre: 
// Post: Function will return true if queue is full, otherwise
// it will return false.
{
	NodeType<C>* location;

	try
	{
		location = new NodeType<C>;
		delete location;
		return false;
	}

	catch(bad_alloc exception)
	{
		return true;
	}

}

template<class C>
bool Queue<C>::isEmpty() const
// Function: Determines if queue is empty or not
// Pre: None
// Post: Function will return true if queue is empty,
// otherwise it will return false.
{
	if(front == NULL)
		return true;
	else
		return false;
}

template<class C>
void Queue<C>::enqueue(C newItem)
// Function: Adds item to the end of the queue
// Pre: Queue has been initialized
// Post: If queue is not full, item is added to the rear 
// of the queue, otherwise a FullQueue exception is thrown.
{
	if(isFull())
		throw FullQueue<C>();
	else
	{
		NodeType<C>* newNode;

		newNode = new NodeType<C>;                                   // Creates new node
		newNode->info = newItem;                                     // Sets info in new node to item
		newNode->next = NULL;                                     // Sets next to NULL

		if (rear == NULL)                                         // If rear of queue is NULL then:
			front = newNode;                                      // Add newNode to front of queue
		else                                                      // Otherwise:
			rear->next = newNode;                                 // Add newNode to end of queue
	}
	length++;
}

template<class C>
void Queue<C>::dequeue(C& item)
// Function: Removes item from front of queue
// Pre: Queue has been intialized
// Post: If queue is not empty, the front of the queue has been
// removed and a copy returned in item. Otherwise, an EmptyQueue
// exception is thrown.
{
	if (isEmpty())
		throw EmptyQueue<C>();
	else
	{
		NodeType<C>* tempPtr;

		tempPtr = front;                                            // Set tempPtr to front
		item = front->info;                                         // Set item to info in front node
		front = front->next;                                        // Set front to next node in queue
		if (front == NULL)                                          // If front of queue is NULL:
			rear = NULL;                                            // Then rear equals NULL
		delete tempPtr;                                             // Deallocate tempPtr
	}
	length--;
}

template<class C>
void Queue<C>::display(ostream& disp) const
{
	NodeType<C>* location = front;
        while(location != NULL)
        {
            disp << location->info << endl;
            location = location->next;
        }
}



Driver.cpp
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Customer.h"
#include "Queue.h"

using namespace std;

int main()
{
	Customer c;
	Queue<Customer> line;
	int simLngth;
	int avgTrans;
	int numServers;
	int avgArvl;
	char answer;
	int count = 1;

	
	// Get simulation parameters
	cout << "Please enter values for the following parameters: " << endl;
	cout << "Simulation length: ";
	cin >> simLngth;
	cout << "Average transaction time: ";
	cin >> avgTrans;
	cout << "Number of servers: ";
	cin >> numServers;
	cout << "Average time between arrivals: ";
	cin >> avgArvl;

	for (int minutes = 1; minutes <= simLngth; minutes++)
	{
		if (minutes % avgArvl == 0)
		{
		      c = Customer(count);
		      line.enqueue(c);
		      count++;
		}
	}
	line.display(cout);
	cout << line.getLength() << endl;
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 04:36 PM

When you add to the queue you should add to the rear. The first time rear is not changed from NULL so the first part of the if statement will always be run. If and when you do add to the end, the rear pointer will need to change.


108        if (rear == NULL)
               // If rear of queue is NULL then:
               // Add newNode to front of queue 
109            front = newNode; 
110        else 
               // Otherwise:
               // Add newNode to end of queue
111            rear->next = newNode;                                
112    }


Was This Post Helpful? 1
  • +
  • -

#8 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 85
  • Joined: 23-July 11

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 05:36 PM

Ok I figured that out I just put rear = newNode; after the if/else statment. Thanks for all the help on this. I'll probably have more questions later (relating more to how to code this assignment because I'm having a lot of trouble with it...it's really hard to come up with code when you've only had about 15-20 hours of sleep over the past 8 days (and btw it was not my fault that I only got that small amount of sleep...I couldn't sleep this week bc my body hates me and decided it wouldn't let me sleep as payback for changing sleeping medication), anyway, look for more topics posted by me throughout the weekend if you want to help out and get lots of reputation! Thanks again for all the help #define and Skydiver!

This post has been edited by HKCHaN92: 22 March 2013 - 05:37 PM

Was This Post Helpful? 1
  • +
  • -

#9 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Linked Queue Display function error cannot convert...

Posted 22 March 2013 - 06:00 PM

View PostHKCHaN92, on 23 March 2013 - 12:36 AM, said:

Ok I figured that out I just put rear = newNode; after the if/else statment.


That's good, I didn't think that far ahead and see it was such a simple change.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1