| | |
anyone plz help me wif dis error?TQ
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
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.cpp
ListMain.cpp
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
c++ Syntax (Toggle Plain Text)
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
c++ Syntax (Toggle Plain Text)
#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
c++ Syntax (Toggle Plain Text)
#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
Last edited by Ancient Dragon; Feb 17th, 2008 at 11:58 pm. Reason: add code tags
Don't use
Always use
In "List.h" the Traverse() method is (incorrectly) prototyped as
Please don't use leetspeak or other weenie talk. (Be kind to those for whom English is not their first language.)
Hope this helps.
void main().Always use
int main().In "List.h" the Traverse() method is (incorrectly) prototyped as
void Traverse( DataType, int& ). In "List.cpp" it is (correctly) defined as bool 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.
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
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Don't usevoid main().
Always useint main().
In "List.h" the Traverse() method is (incorrectly) prototyped asvoid Traverse( DataType, int& ). In "List.cpp" it is (correctly) defined asbool 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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> ... so, what should i change in the coding to solve the problem?
change the return type from void to bool
change the return type from void to bool
template < class DataType>
class List
{
// ...
bool Traverse(DataType, int &);
// ...
};•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
> ... 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
Alas, (I hope you are sitting down), the error is explained here:
C++ FAQ Lite: Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?
I've never encountered this problem before (I've never tried to do what you are doing) but I'll poke around a bit and see if there is a solution.
Unless, of course, someone else here knows the answer...
C++ FAQ Lite: Why am I getting errors when my template-derived-class uses a nested type it inherits from its template-base-class?
I've never encountered this problem before (I've never tried to do what you are doing) but I'll poke around a bit and see if there is a solution.
Unless, of course, someone else here knows the answer...
Last edited by Duoas; Feb 18th, 2008 at 1:06 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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:
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).
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:
c++ Syntax (Toggle Plain Text)
#include "List.h" export template<class DataType> List<DataType>::List() { numItem = 0; pHead = 0; } // etc
a practical solution would be: move the definitions from List.cpp to List.h (and throw the now empty List.cpp away).
![]() |
Other Threads in the C++ Forum
- Previous Thread: c++ time
- Next Thread: Regarding C++ static analysis?
Views: 1524 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






