hello

I have an error with template class and I can not understand what is the problem?
the code illustrate the arrangement of many of books by its publishers' names and if the books have same publisher arrange them by the title of the book .

the error is
instantiated from `int List<TYPE, KTYPE>::addNode(TYPE) [with TYPE = book, KTYPE = std::string]'

#include<string>
using namespace std;
#include<iostream.h>

struct book
{
	string key1;
	string key2;
	string Title;
	string Author;
	string Publisher;
	
	
};

//	Node Declaration
	template <class  TYPE> 
	struct NODE 
	  {
	   	TYPE    data;
	   	NODE   *link;
	  }; //  End of Node Declaration

//	List Class Declaration

	template <class  TYPE, class KTYPE> 
	class List  
	   {
	     private:
	       NODE<TYPE> *head;
	       NODE<TYPE> *pos;
	       NODE<TYPE> *rear;
	       int         count;

//	     Function Declarations
	       bool  _insert  (NODE<TYPE>   *pPre,TYPE          dataIn);
	      // void  _delete  (NODE<TYPE>   *pPre,NODE<TYPE>   *pLoc,TYPE	*dataOutPtr);
	       bool  _search  (NODE<TYPE>  **pPre, NODE<TYPE>  **pLoc,KTYPE  key,KTYPE  key1);

	     public:
	       List (void);
	    
	       int   addNode (TYPE   dataIn);
	     
	   }; // class List 

// 	End of List Class Declaration

/* 	=============== List Constructor  ==============	
	Initialize the list.
	  Pre    Class is being instantiated
	  Post   Class instantiated and initialized
*/

template <class TYPE, class KTYPE>
List<TYPE, KTYPE> :: List (void)   
{
//	Statements 
	head     = NULL;
	pos      = NULL;
	rear     = NULL;
	count    = 0;
} //  List Constructor 


/*	==================== addNode =================== 
	Inserts data into linked list.
	   Pre     dataIn contains data to be inserted
	   Post    Data inserted or error
	   Return -1 if overflow, 
	           0 if successful,
	           1 if duplicate key
*/
template <class TYPE, class KTYPE> 
int List<TYPE, KTYPE> :: addNode (TYPE dataIn)
{
//	Local Definitions 
	bool  found;
	bool  success;

	NODE<TYPE>  *pPre;
	NODE<TYPE>  *pLoc;
	
//  Statements 
	found = _search (&pPre, &pLoc, dataIn.key1,dataIn.key2);
	if (found)
	   // Duplicate keys not allowed 
	   return (+1);
	
	success = _insert (pPre,  dataIn);
	if (!success)
	   // Overflow 
	   return (-1);
	return (0);
}	//  addNode 

/*	===================== _insert ==================== 
	Inserts data into a new node in the linked list.
	   Pre     Insertion location identified by pPre
	           dataIn contains data to be inserted
	   Post    data inserted in linked list or overflow
	   Return  true  if successful, false if overflow
*/
template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: _insert (NODE<TYPE> *pPre,TYPE        dataIn)
{
//	Local Definitions 
	NODE <TYPE>  *pNew;

//	Statements 
	if (! (pNew = new NODE<TYPE>))
	   return false;
	
	pNew->data = dataIn; 
	pNew->link = NULL; 
		 
	if (pPre == NULL)
	   {
	    //  Adding before first node or to empty list. 
	    pNew->link = head;
	    head = pNew;
	   } // if pPre 
	else
	    {
	     // Adding in middle or at end 
	     pNew->link  = pPre->link;
	     pPre->link  = pNew;
	    } // if else  
	 
	     // Now check for add at end of list 
	     if (pNew->link == NULL)
	        // Adding to empty list. Set rear  
	        rear = pNew;

	count++;

	return true;
}	// _insert 


/*	==================== _search =================== 
	Searches list and passes back address of node 
	containing target and its logical predecessor.
	   Pre    pPre is pointer variable for predecessor
	          pLoc is pointer variable for found node
	          key  is search argument
	   Post   pLoc points to first node equal/greater key 
	          -or- null if target > key of last node
	          pPre points to largest node smaller than key
	          -or- null if target < key of first node
	   Return true if successful, false if not found
*/
template <class TYPE, class KTYPE> 
bool List<TYPE, KTYPE> :: _search  (NODE<TYPE>  **pPre, NODE<TYPE>  **pLoc,KTYPE key,KTYPE  key1)
{
//	Statements 
	*pPre  = NULL;
	*pLoc  = head;
	if (count == 0)
	    return false;
	
	// Test for argument > last node in list 
	if (key > rear->data.key1) 
	   {
	    *pPre = rear;
	    *pLoc = NULL;
	    return false;
	   } // if 

	while (key > (*pLoc)->data.key1)
	   {
	    //  Have not found search argument location 
	    *pPre = *pLoc;
	    *pLoc = (*pLoc)->link;
	   } // while 
	
	if (key == (*pLoc)->data.key1)//   argument found--success
	{
		if(key1== (*pLoc)->data.key2)
	    return true;
	}
	else
	    return false;
}	//  _search 






int main()
{
	
	book obj,obj1;
	
	obj.Title="c++";
	obj.Author="jone";
	obj.Publisher="Addison Wesley";
	obj.key1=obj.Publisher;
	obj.key2=obj.Title;
	
	obj1.Title="c++";
	obj1.Author="jone";
	obj1.Publisher="Addison Wesley";
	obj1.key1=obj1.Publisher;
	obj1.key2=obj1.Title;
	
	List<book,string>list;
	cout<<list.addNode(obj);
	cout<<list.addNode(obj1);
	
	
	return 0;
}

can any one help me with some explanation?
the code is written in Eclipse

sorry for weakly language,my native language is not english
Thank you so much

Recommended Answers

All 2 Replies

instead of this,

#include<string>
using namespace std;
#include<iostream.h> // no such header

write

#include<string>
#include<iostream> // the correct header
using namespace std; // moved down

also,

template <class TYPE, class KTYPE>
bool List<TYPE, KTYPE> :: _search (NODE<TYPE> **pPre, NODE<TYPE> **pLoc,KTYPE key,KTYPE key1)
{
  // Statements
  *pPre = NULL;
  *pLoc = head;
  if (count == 0)
  return false;

  // Test for argument > last node in list
  if (key > rear->data.key1)
  {
    *pPre = rear;
    *pLoc = NULL;
    return false;
  } // if

  while (key > (*pLoc)->data.key1)
  {
    // Have not found search argument location
    *pPre = *pLoc;
    *pLoc = (*pLoc)->link;
  } // while

  if (key == (*pLoc)->data.key1)// argument found--success
  {
    if(key1== (*pLoc)->data.key2)
    return true;
    // else 
       // what do you want to return? true or false?
  }
  else
    return false;
  // **** error: control reaches end of non-void function.
} // _search

I think that you should try to put template<class TYPE> before EVERY function which is uses TYPE.
KTYPE analogical.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.