Solved

C 2804

Posted on 2011-03-18
4
221 Views
Last Modified: 2012-05-11
Hello Everyone,

I am creating a Linked List class and within that linked list class is the ListNode class.  Within the ListNode class is a data member which is a vector node_linked_list.  I have a member function  displayList which will display the entire vector node_linked_list.  To display this I overloaded the operator <<.  But I am getting an error C2804  binary operator ' <<' has too many parameters.  
template<class Object>
class ListNode
{
public:
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;
	}

	template<class Object>
	ostream &operator<<(ostream& out, ListNode<Object> &node_linked_list)
	{
		out<<node_linked_list;
		return out;
	}

Open in new window

0
Comment
Question by:brich744
  • 2
  • 2
4 Comments
 

Author Comment

by:brich744
Comment Utility
Here is the entire LinkedList and LinkedNode class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class Object>
class ListNode
{
public:
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;
	}

	template<class Object>
	ostream &operator<<(ostream& out, ListNode<Object> &node_linked_list)
	{
		out<<node_linked_list;
		return out;
	}


	void set_rangeNumber();
	Object get_rangeNumber();

	void displayList();



	Object value;
	ListNode<Object> *next;
	vector<Object> *node_linked_list;
private:
	 Object start_blockValue;
	 Object end_blockValue;

};



template<class Object>
void ListNode<Object>::displayList()
{
	for(int i = 0; i != node_linked_list->size(); i++)
			cout<<node_linked_list[i];
}

template<class Object>
void ListNode<Object>::set_rangeNumber()
{
	start_blockValue += 32;
}

template<class Object>
Object ListNode<Object>::get_rangeNumber()
{
	return start_blockValue;
}

template<class Object>
class LinkedList
{

private:
	ListNode<Object> *head;
	 int total_free_memory;
	 int total_used_memory;

public:
	LinkedList()
	{
		head = NULL;
		total_free_memory = 0;
		total_used_memory = 0;
	}

	~LinkedList();
	void appendNode(Object);
	void appendNode(Object, int, int);

	void insertNode(Object);
	void show_List(string);
	void find_value(Object);
	void deleteNode(Object);
	
	void add_free_memory(Object);
	void add_used_memory(Object);
	void set_memory(Object);

	Object getTotal_free_memory();
	Object getTotal_used_memory();

};

template<class Object>
void LinkedList<Object>::add_free_memory(Object value)
{
	free_memory += value;
	used_memory -= value;
}

template<class Object>
void LinkedList<Object>::add_used_memory(Object value)
{
	used_memory += value;
	free_memory -= value;
}

template<class Object>
void LinkedList<Object>::set_memory(Object value)
{
	total_free_memory = value;
}

template<class Object>
Object LinkedList<Object>::getTotal_free_memory()
{
	return total_free_memory;
}

template<class Object>
Object LinkedList<Object>::getTotal_used_memory()
{
	return used_memory;
}


template<class Object>
void LinkedList<Object>::appendNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr;

	newNode = new ListNode<Object>(newValue);

	if(head == NULL)
		head = newNode;

	else
	{
		nodePtr = head;

		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::appendNode(Object newValue, int startValue, int endValue)
{
	ListNode<Object> *newNode, *nodePtr;
	
	//Allocate a new node and store the value
	newNode = new ListNode<Object>(newValue, startValue, endValue);

	//If there are no nodes in the list
	//make newNode the first node
	if(head == NULL)
		head = newNode;
	//Insert the node at the end of the list
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Find the last node in the list
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::show_List(string name)
{
	cout<<"*****"<<name<<"*****"<<endl;

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		cout<<nodePtr->value<<": ";

		nodePtr->displayList();

		nodePtr = nodePtr->next;
	}

	cout<<""<<endl;
}

template<class Object>
void LinkedList<Object>::find_value(Object memory)
{
	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}		

	}

}

template<class Object>
void LinkedList<Object>::insertNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr, *previousNode = NULL;

	//Allocate a new node and store newValue
	newNode = new ListNode<Object>(newValue);

	//If there are no nodes in the  list
	//make newNode the first node

	if(head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		//Initialize nodePtr to head of list and previousNode to NULL
		nodePtr = head;
		previousNode = NULL;

		//Skip all nodes whose value member is less than newValue
		while(nodePtr != NULL && nodePtr->value < newValue)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If the new node is the first in the list
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next=nodePtr;
		}

		else
		{
			previousNode->next=newNode;
			newNode->next=nodePtr;
		}
	}
}

template<class Object>
void LinkedList<Object>::deleteNode(Object searchValue)
{
	ListNode<Object> *nodePtr, *previousPtr;

	//If the list is empty do nothing
	if(head == NULL)
		return;

	//Determine if the first node is the value that we are looking for
	if(head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Skip all nodes whose value member is not equal to search value
		while(nodePtr != NULL && nodePtr->value != searchValue)
		{
			previousNode=nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr is @ the end of the list
		//than link the previous node to the node after nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

template<class Object>
LinkedList<Object>::~LinkedList()
{
	ListNode<Object> *nodePtr, *nextNode;

	nodePtr = head;
	while(nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;

	}
}

#endif

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, the parameter list would be OK for an operator at global scope (i.e. not a member), which I'd recommend in this case (BTW, make the object passed in a const reference):
// foward declarations

template<class Object> class ListNode;
template<class Object>
ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list);

//...

template<class Object>
class ListNode
{

friend template<class Object>
	ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list);

public:
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;
	}

// ...

};

template<class Object>
ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list)
{
	out<<node_linked_list.value;
	return out;
}

Open in new window

0
 

Author Comment

by:brich744
Comment Utility
So I have changed the code and now I am getting the error C2649: 'typename' is not a class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class Object> class ListNode;
template<class Object>
ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list);

template<class Object>
class ListNode
{
	friend template<class Object>
	ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list);

public:
	
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;
	}

	

	void set_rangeNumber();
	Object get_rangeNumber();

	void displayList();



	Object value;
	ListNode<Object> *next;
	vector<Object> *node_linked_list;
private:
	 Object start_blockValue;
	 Object end_blockValue;

};

template<class Object>
ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list)
{
	out<<node_linked_list.value;
	return out;
}

template<class Object>
void ListNode<Object>::displayList()
{
	for(int i = 0; i != node_linked_list->size(); i++)
			cout<<node_linked_list[i];
}

template<class Object>
void ListNode<Object>::set_rangeNumber()
{
	start_blockValue += 32;
}

template<class Object>
Object ListNode<Object>::get_rangeNumber()
{
	return start_blockValue;
}

template<class Object>
class LinkedList
{

private:
	ListNode<Object> *head;
	 int total_free_memory;
	 int total_used_memory;

public:
	LinkedList()
	{
		head = NULL;
		total_free_memory = 0;
		total_used_memory = 0;
	}

	~LinkedList();
	void appendNode(Object);
	void appendNode(Object, int, int);

	void insertNode(Object);
	void show_List(string);
	void find_value(Object);
	void deleteNode(Object);
	
	void add_free_memory(Object);
	void add_used_memory(Object);
	void set_memory(Object);

	Object getTotal_free_memory();
	Object getTotal_used_memory();

};

template<class Object>
void LinkedList<Object>::add_free_memory(Object value)
{
	free_memory += value;
	used_memory -= value;
}

template<class Object>
void LinkedList<Object>::add_used_memory(Object value)
{
	used_memory += value;
	free_memory -= value;
}

template<class Object>
void LinkedList<Object>::set_memory(Object value)
{
	total_free_memory = value;
}

template<class Object>
Object LinkedList<Object>::getTotal_free_memory()
{
	return total_free_memory;
}

template<class Object>
Object LinkedList<Object>::getTotal_used_memory()
{
	return used_memory;
}


template<class Object>
void LinkedList<Object>::appendNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr;

	newNode = new ListNode<Object>(newValue);

	if(head == NULL)
		head = newNode;

	else
	{
		nodePtr = head;

		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::appendNode(Object newValue, int startValue, int endValue)
{
	ListNode<Object> *newNode, *nodePtr;
	
	//Allocate a new node and store the value
	newNode = new ListNode<Object>(newValue, startValue, endValue);

	//If there are no nodes in the list
	//make newNode the first node
	if(head == NULL)
		head = newNode;
	//Insert the node at the end of the list
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Find the last node in the list
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::show_List(string name)
{
	cout<<"*****"<<name<<"*****"<<endl;

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		cout<<nodePtr->value<<": ";

		nodePtr->displayList();

		nodePtr = nodePtr->next;
	}

	cout<<""<<endl;
}

template<class Object>
void LinkedList<Object>::find_value(Object memory)
{
	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}		

	}

}

template<class Object>
void LinkedList<Object>::insertNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr, *previousNode = NULL;

	//Allocate a new node and store newValue
	newNode = new ListNode<Object>(newValue);

	//If there are no nodes in the  list
	//make newNode the first node

	if(head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		//Initialize nodePtr to head of list and previousNode to NULL
		nodePtr = head;
		previousNode = NULL;

		//Skip all nodes whose value member is less than newValue
		while(nodePtr != NULL && nodePtr->value < newValue)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If the new node is the first in the list
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next=nodePtr;
		}

		else
		{
			previousNode->next=newNode;
			newNode->next=nodePtr;
		}
	}
}

template<class Object>
void LinkedList<Object>::deleteNode(Object searchValue)
{
	ListNode<Object> *nodePtr, *previousPtr;

	//If the list is empty do nothing
	if(head == NULL)
		return;

	//Determine if the first node is the value that we are looking for
	if(head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Skip all nodes whose value member is not equal to search value
		while(nodePtr != NULL && nodePtr->value != searchValue)
		{
			previousNode=nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr is @ the end of the list
		//than link the previous node to the node after nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

template<class Object>
LinkedList<Object>::~LinkedList()
{
	ListNode<Object> *nodePtr, *nextNode;

	nodePtr = head;
	while(nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;

	}
}

#endif

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Sorry, a copy&paste acccident on my side - that should be
template<class Object>
class ListNode
{
	friend ostream &operator<<(ostream& out, const ListNode<Object> &node_linked_list);

public:
	
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;
	}

	

	void set_rangeNumber();
	Object get_rangeNumber();

	void displayList();



	Object value;
	ListNode<Object> *next;
	vector<Object> *node_linked_list;
private:
	 Object start_blockValue;
	 Object end_blockValue;

};

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now