Hello there ive written my own linklist in a template way but it doesnt seem to want to work when i add classes rather than primitive data types to the list...

heres the code:

[the node itself (template)

#ifndef LINKLISTNODETEMPLATE_H
#define LINKLISTNODETEMPLATE_H


template <class T> class LinkListNodeTemplate
{
	public :
		T data;
		LinkListNodeTemplate * nextElement;

		LinkListNodeTemplate () 
		{
			nextElement=0;
		}
};

#endif

[heres the linklist template]

#ifndef LINKLISTTEMPLATE_H
#define LINKLISTTEMPLATE_H

#include "LinkListNodeTemplate.h"

template <class T>class LinkListTemplate {
public:
	LinkListTemplate(void)
	{
		head=0;
	}

	~LinkListTemplate(void)
	{
	}
	
	void addNode(LinkListNodeTemplate<T> *node)
	{
	
	//Add the node to the list
	if(head == 0)
	{
		head = node;
	} 
	else 
	{
		LinkListNodeTemplate<T> *iteratorNode = head;

		//transerve through the list until end
		while(iteratorNode->nextElement != 0) 
		{
			iteratorNode = iteratorNode->nextElement;
		}
		iteratorNode->nextElement=node;
	
	}
}
	

void print() 
{
	LinkListNodeTemplate<T> *iteratorNode = head;

	while(iteratorNode != 0)
	{
		cout << iteratorNode->data << endl;
		iteratorNode = iteratorNode->nextElement;
	}
}

//private :
	LinkListNodeTemplate<T> *head;

};


#endif

however when i add a type of this class to the list :

#ifndef LINKLISTNODE_H
#define LINKLISTNODE_H

class LinkListNode {

public :
	int number;
	//LinkListNode * nextElement;

	inline LinkListNode()
	{
		number = 0;
	}
};

#endif

i get a series of errors:

Main.cpp
c:\documents and settings\john\desktop\year 4\ape\linklist\linklist\linklisttemplate.h(47) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'LinkListNode' (or there is no acceptable conversion)
c:\program files\microsoft visual studio 8\vc\include\ostream(650): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(697): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(735): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(782): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,char)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(906): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const signed char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(913): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,signed char)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(920): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const unsigned char *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(927): or 'std::basic_ostream<_Elem,_Traits> &std::operator <<<std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,unsigned char)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(168): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits> &(__cdecl *)(std::basic_ostream<_Elem,_Traits> &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(174): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(181): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::ios_base &(__cdecl *)(std::ios_base &))'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(188): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::_Bool)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(208): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(short)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(241): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned short)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(261): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(int)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(286): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned int)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(306): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(326): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__w64 unsigned long)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(347): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(__int64)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(367): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned __int64)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(388): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(float)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(408): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(double)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(428): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long double)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(448): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(const void *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 8\vc\include\ostream(468): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_streambuf<_Elem,_Traits> *)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
while trying to match the argument list '(std::ostream, LinkListNode)'
c:\documents and settings\john\desktop\year 4\ape\linklist\linklist\linklisttemplate.h(42) : while compiling class template member function 'void LinkListTemplate<T>::print(void)'
with
[
T=LinkListNode
]
c:\documents and settings\john\desktop\year 4\ape\linklist\linklist\main.cpp(37) : see reference to class template instantiation 'LinkListTemplate<T>' being compiled
with
[
T=LinkListNode
]
Build log was saved at "file://c:\Documents and Settings\John\Desktop\Year 4\APE\LinkList\LinkList\Debug\BuildLog.htm"
LinkList - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

any suggestions?

The problem isn't with adding or deleting nodes, it is with using the operator <<. When you say

cout << iteratorNode->data << endl;

you need to be aware of what type of data data is. If it is a simple type (int, float, etc.) or a predefined type (std::string, etc.) then the left-shift operator has already been properly overloaded to send data to cout.

In your case, you have added a node type that does not tell the compiler how to "left-shift" data into the cout stream.

Try adding the following to your node definition:

ostream &operator << ( ostream &outs, const LinkListNode &node ) {
  cout << node.number;
  return outs;
  }

Now, with the operator properly overloaded you shouldn't have any trouble.

PS. Don't use using namespace std; unless you are editing your program's main source file. Library files (such as your linked list node) should refer to the desired namespace explicitly:

std::cout << "fooey" << std::endl;

Doing this will save you a lot of grief when your project gets large.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.