Solved

C 2804

Posted on 2011-03-18
4
246 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
ID: 35167760
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
ID: 35167894
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
ID: 35169580
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
ID: 35169614
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
valid enum? 6 74
gdb doesn't stop on breakpoint 2 69
delete-remove 14 87
Display a Float Variable in C without using the function printf. 2 14
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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