i hav write a coding regarding to lists topic. and i had found about 3 error which i cant solve. any1 here who can help me wif dis error? i m very appreciate 4 all of ur help..TQ..dis is my coding and the error..TQ

List.h

#ifndef LIST_H
#define LIST_H

template < class DataType>
class List
{
private:
	class Node
	{
	public:
		DataType data;
		Node * link;
	};

	Node *pHead;
	Node *pCurr;
	int numItem;

public:
	List();
	~List();
	void AddToFront();
	void Traverse(DataType, int &);
	void printData();
	int NumberOfItem();
};
#endif

List.cpp

#include <iostream>
using namespace std;
#include "List.h"

template<class DataType>
List<DataType>::List()
{
	numItem = 0;
	pHead = 0;
}

template <class DataType>
List<DataType>::~List()  {}

template <class DataType>
void List<DataType>::AddToFront()
{
	DataType item;
	Node *pNew = new Node;
	cout << "Enter data : ";
	cin >> item;
	pNew->data = item;
	pNew->link = pHead;
	pHead = pNew;
	numItem++;
}

template <class DataType>
void List <DataType>::printData()
{
	pCurr = pHead;
	while (pCurr != 0)
	{
		cout << pCurr->data << " ";
		pCurr = pCurr->link;
	}
	cout << "\n";
 }

template <class DataType>
int List<DataType>::NumberOfItem()
{
return numItem;
}

template <class DataType>
bool List<DataType>::Traverse (DataType target, int &loc)
{
	if (numItem == 0)
		cout <<"There is no item in the list" <<endl;
	else
	{
		pCurr = pHead;
		loc = 0;
		while (pCurr -> data != target && pCurr -> link != 0)
		{
			pCurr = pCurr -> link;
			loc++;
		}
		if (pCurr -> data == target)
			return true;
		else
			return false;
	}

}

ListMain.cpp

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

void main()
{
	int target;
	int location;
	
	List<int> list;
	for (int i = 1; i <4; i++)
	{
		list.AddToFront();
	}

	cout << "\nNumber of Item Now: " <<list.NumberOfItem();
	cout << "\nThe list are : " <<endl;
	list.printData();

	cout<<"\nEnter the search item : ";
	cin>> target;

	if (list.Traverse(target, location) == true)
	{
		cout<< "Item is found at location : "
			<<location<<endl;
	}
	else
	{
		cout << "Item is not found  \n";
	}
}

ERROR:

c:\Documents and Settings\VeNTRiCaLiM\Desktop\assignment5\6example1\ListMain.cpp(24): warning C4805: '==' : unsafe mix of type 'void' and type 'bool' in operation


c:\Documents and Settings\VeNTRiCaLiM\Desktop\assignment5\6example1\ListMain.cpp(24): error C2120: 'void' illegal with all types


c:\Documents and Settings\VeNTRiCaLiM\Desktop\assignment5\6example1\List.cpp(66): error C2244: 'List<DataType>::Traverse' : unable to match function definition to an existing declaration

can tel me where should i change in order to solve the error?TQ

Don't use void main() .
Always use [B]int[/B] main() .

In "List.h" the Traverse() method is (incorrectly) prototyped as void Traverse( DataType, int& ) . In "List.cpp" it is (correctly) defined as [B]bool[/B] Traverse( DataType, int& ) .

Please don't use leetspeak or other weenie talk. (Be kind to those for whom English is not their first language.)

Hope this helps.

Comments
:)

A different translator's versions of the error messages:

list.cpp
*** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is ON! ***
list.cpp:50: error: prototype for
`bool List<DataType>::Traverse(DataType, int &)' does not match any in
class `List<DataType>'
List.h:23: error: candidate is: void List<DataType>::Traverse(DataType, int &)
list.cpp:50: error: template definition of non-template
`bool List<DataType>::Traverse(DataType, int &)'
main.cpp
*** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is ON! ***
main.cpp:7: error: `main' must return `int'
main.cpp: In function `int main(...)':
main.cpp:24: error: void value not ignored as it ought to be

Don't use void main() .
Always use [B]int[/B] main() .

In "List.h" the Traverse() method is (incorrectly) prototyped as void Traverse( DataType, int& ) . In "List.cpp" it is (correctly) defined as [B]bool[/B] Traverse( DataType, int& ) .

Please don't use leetspeak or other weenie talk. (Be kind to those for whom English is not their first language.)

Hope this helps.

Thanks for your reply. you means in "List.h", the Traverse() method is incorrectly, so, what should i change in the coding to solve the problem? Somemore, in "List.cpp" , is it have to change the traverse method also?thank you for your kindly reply.

> ... so, what should i change in the coding to solve the problem?
change the return type from void to bool

template < class DataType>
class List
{
    // ...
    bool Traverse(DataType, int &);
    // ...
};

> ... so, what should i change in the coding to solve the problem?
change the return type from void to bool

template < class DataType>
class List
{
    // ...
    bool Traverse(DataType, int &);
    // ...
};

i change already and more errors come out. this is the error:

6example1 error LNK2019: unresolved external symbol "public: __thiscall List<int>::~List<int>(void)" (??1?$List@H@@QAE@XZ) referenced in function _main


6example1 error LNK2019: unresolved external symbol "public: bool __thiscall List<int>::Traverse(int,int &)" (?Traverse@?$List@H@@QAE_NHAAH@Z) referenced in function _main


6example1 error LNK2019: unresolved external symbol "public: void __thiscall List<int>::printData(void)" (?printData@?$List@H@@QAEXXZ) referenced in function _main


6example1 error LNK2019: unresolved external symbol "public: int __thiscall List<int>::NumberOfItem(void)" (?NumberOfItem@?$List@H@@QAEHXZ) referenced in function _main


6example1 error LNK2019: unresolved external symbol "public: void __thiscall List<int>::AddToFront(void)" (?AddToFront@?$List@H@@QAEXXZ) referenced in function _main


6example1 error LNK2019: unresolved external symbol "public: __thiscall List<int>::List<int>(void)" (??0?$List@H@@QAE@XZ) referenced in function _main


6example1 fatal error LNK1120: 6 unresolved externals


so..what should i do?Thanks

the functions for which you get a linker error are only declared in List.h. they are defined in List.cpp.

the pedantic answer would be: to use templates without their definition being visible, they must be exported. ie. in List.cpp, they must be defined this way:

#include "List.h"
export template<class DataType>
List<DataType>::List()
{
	numItem = 0;
	pHead = 0;
}
// etc

however, most current compilers do not support the export keyword. ( Comeau C++ being the notable exception)

a practical solution would be: move the definitions from List.cpp to List.h (and throw the now empty List.cpp away).

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