[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

C 2804

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
brich744
Asked:
brich744
  • 2
  • 2
1 Solution
 
brich744Author Commented:
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
 
jkrCommented:
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
 
brich744Author Commented:
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
 
jkrCommented:
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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now