Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C 2804

Posted on 2011-03-18
4
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

618 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