| | |
Error in c++ program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
I have design two program and both have errors on it. the first is supposed to handle queue on integer, double, string and date. I have done for integer and double. When I compile it there was error on it. If you can just help me debug it and i will complete the string and date myself. i have to submit this by Thursday. below is the code for it:
Thank you for your help.
C++ Syntax (Toggle Plain Text)
//listnd.h //ListNode template definition #ifndef LISTND_H #define LISTND_H template< class NODETYPE > class List; //forward declaration template<class NODETYPE> class ListNode { friend class List< NODETYPE >; //make List a friend public: ListNode( const NODETYPE & ); //constructor NODETYPE getData() const; //return data in the node private: NODETYPE data; //data ListNode< NODETYPE > *nextPtr; //next node in the list }; //Constructor template<class NODETYPE> ListNode< NODETYPE >::ListNode( const NODETYPE &info ) : data( info ), nextPtr( 0 ) { } //Return a copy of the data in the node template< class NODETYPE > NODETYPE ListNode< NODETYPE >::getData() const { return data; } #endif //------------------------------------------------------------------------- #ifndef LIST_H #define LIST_H #include <iostream> #include <cassert> #include "listnd.h" using std::cout; template< class NODETYPE > class List { public: List(); //constructor ~List(); //destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack( NODETYPE & ); bool isEmpty() const; void print() const; private: ListNode< NODETYPE > *firstPtr; //pointer to first node ListNode< NODETYPE > *lastPtr; //pointer to last node //Utility function to allocate a new node ListNode< NODETYPE > *getNewNode( const NODETYPE & ); }; //Default constructor template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { } //Destructor template< class NODETYPE > List< NODETYPE >::~List() { if( !isEmpty() ) { //List is not empty cout << "Destroying nodes...\n"; ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr; while ( currentPtr != 0 ) { //delete remaining nodes tempPtr = currentPtr; cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } } cout << "All nodes destroyed\n\n"; } //Insert a node at the front of the list template< class NODETYPE > void List< NODETYPE>::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) //List is empty firstPtr = lastPtr = newPtr; else { //List is not empty newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } //Insert a node at the back of the list template< class NODETYPE > void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if (isEmpty() ) //List is empty firstPtr = lastPtr = newPtr; else { //List is not empty lastPtr->nextPtr = newPtr; lastPtr = newPtr; } } //Delete a node from the front of the list template< class NODETYPE > bool List< NODETYPE >::removeFromFront( NODETYPE &value ) { if ( isEmpty() ) //List is empty return return; //delete unsuccessful else { ListNode< NODETYPE > *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; //data being removed delete tempPtr; return; //delete successful } } //Delete a node from the back of the list template< class NODETYPE > bool List< NODETYPE >::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return return; //delete succesful else { ListNode< NODETYPE > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = LastPtr = 0; else { ListNode< NODETYPE > *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr; return return; //delete succesful } } //Is the List empty template< class NODETYPE > bool List< NODETYPE >::isEmpty() const { return firstPtr ==0; } //return a pointer to a newly allocated node template < class NODETYPE > ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { ListNode< NODETYPE > *ptr = new ListNode< NODETYPE >( value ); assert( ptr != 0 ); return ptr; } //Display the contents of the List template< class NODETYPE > void List< NODETYPE >::print() const { if ( isEmpty() ) { cout << "The list is empty\n\n"; return; } ListNode< NODETYPE > *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) { cout << currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } cout << "\n\n"; } #endif //------------------------------------------------------------------------- #ifndef QUEUE_H #define QUEUE_H #include "list.h" template< class QUEUETYPE > class Queue: private List< QUEUETYPE > { public: void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); } bool dequeue( QUEUETYPE &d ) { return removeFromFront( d ); } bool isQueueEmpty() const { return isEmpty(); } void printQueue() const { print(); } }; #endif //------------------------------------------------------------------------- #include <iostream> #include "queue.h" using std::endl; int main() { Queue< int > intQueue; int dequeueInteger, i; cout << "processing an integer Queue" << endl; for ( i = 0; i < 4; i++ ) { intQueue.enqueue( i ); intQueue.printQueue(); } while ( !intQueue.isQueueEmpty() ) { intQueue.dequeue( dequeueInteger ); cout << dequeueInteger << " dequeued" << endl; intQueue.printQueue(); } Queue< double > doubleQueue; double val = 1.1, dequeueDouble; cout << "processing a double Queue" << endl; for ( i = 0; i < 4; i++ ) { doubleQueue.enqueue( val ); doubleQueue.printQueue(); val += 1.1; } while ( !doubleQueue.isQueueEmpty() ) { doubleQueue.dequeue( dequeueDouble ); cout << dequeueDouble << " dequeued" << endl; doubleQueue.printQueue(); } return 0; } The second problem is a program that is supposed to do a quicksort on a set of array type double and later do a bsearch. I actually modified the actual program into a class file. I'm actually weak at class and i hope you can help me. Below is the code: #include <iostream.h> #include <algorithm> //class quicksort class qsort { public: int compare(const void*, const void*); //qsort (list, 10, sizeof(int), compare); //error public: int list; int element; }; //class bsearch class bsearch { public: int compare(const void*, const void*); public: double list; int element; double key; }; int main() { double list[10] = {9.2,5.1,2.6,8.5,1.7,7.1,3.2,6.3,4.6,0.1}; double key = 3; //display unsorted list for (int element = 0; element <= 9; element++) { cout << list[element] << " "; } cout << endl; qsort (list, 10, sizeof(int), compare); //error //display sorted list for (element = 0; element <= 9; element++) { cout << list[element] << " "; } //displays whether the number in the array or not /*if (bsearch(&key,list,10,sizeof(int),compare)) //error { cout<<key<<" is in the array"<<endl; //number in array } else { cout << key <<" is NOT in the array2" <<endl; //number not in array }*/ return 0; } int compare1(const void* a_PTR, const void* b_PTR) { return *((int*)a_PTR) - *((int*)b_PTR); }
Last edited by alc6379; Oct 26th, 2004 at 12:17 pm.
>If you can just help me debug it
I'll help, but I'm not going to do it for you. Debugging is what the majority of programming is, so if I did it for you then you would learn nothing. As it is, you didn't specify what the error was in the first program, and I have better things to do than figure out what you think is wrong with it then tell you so that you can claim m4d debugging sk1llz. :rolleyes:
>qsort (list, 10, sizeof(int), compare); //error
You can't call a function in a class definition.
>if (bsearch(&key,list,10,sizeof(int),compare)) //error
Is the error "Unrecognized function" or something similar? Tell us what the errors are! And include <cstdlib> because that's where qsort and bsearch are declared.
I'll help, but I'm not going to do it for you. Debugging is what the majority of programming is, so if I did it for you then you would learn nothing. As it is, you didn't specify what the error was in the first program, and I have better things to do than figure out what you think is wrong with it then tell you so that you can claim m4d debugging sk1llz. :rolleyes:
>qsort (list, 10, sizeof(int), compare); //error
You can't call a function in a class definition.
>if (bsearch(&key,list,10,sizeof(int),compare)) //error
Is the error "Unrecognized function" or something similar? Tell us what the errors are! And include <cstdlib> because that's where qsort and bsearch are declared.
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
For the first problem that is about queues. I have debug it and when I compile the program there is no error or warning but when i build it it says 3 errors. Below is the modified code and the errors
Code:
Error:
1. T2.OBJ : error LNK2001: unresolved external symbol "public: bool __thiscall List<int>::removeFromFront(int &)" (?removeFromFront@?$List@H@@QAE_NAAH@Z)
2. T2.OBJ : error LNK2001: unresolved external symbol "public: bool __thiscall List<double>::removeFromFront(double &)" (?removeFromFront@?$List@N@@QAE_NAAN@Z)
3. Debug/T2.exe : fatal error LNK1120: 2 unresolved externals
Honestly speaking i don't have a clue what this means. I tried going through the whole program again and again. Checking through to see what the problem and i can't find it. ( note: T2 is the name of the program. T2.cpp ). Anyone can explain to me on how to handle this?
:mad:
Code:
C++ Syntax (Toggle Plain Text)
//listnd.h //ListNode template definition #ifndef LISTND_H #define LISTND_H template< class NODETYPE > class List; // forward declaration template<class NODETYPE> class ListNode { friend class List< NODETYPE >; // make List a friend public: ListNode( const NODETYPE & ); // constructor NODETYPE getData() const; // return data in the node private: NODETYPE data; // data ListNode< NODETYPE > *nextPtr; // next node in the list }; // Constructor template<class NODETYPE> ListNode< NODETYPE >::ListNode( const NODETYPE &info ) : data( info ), nextPtr( 0 ) { } // Return a copy of the data in the node template< class NODETYPE > NODETYPE ListNode< NODETYPE >::getData() const { return data; } #endif // list.h // Template List class definition #ifndef LIST_H #define LIST_H #include <iostream> #include <cassert> #include "listnd.h" using std::cout; template< class NODETYPE > class List { public: List(); // constructor ~List(); // destructor void insertAtFront( const NODETYPE & ); void insertAtBack( const NODETYPE & ); bool removeFromFront( NODETYPE & ); bool removeFromBack ( NODETYPE & ); bool isEmpty() const; void print() const; private: ListNode< NODETYPE > *firstPtr; // pointer to first node ListNode< NODETYPE > *lastPtr; // pointer to last node // Utility function to allocate a new node ListNode< NODETYPE > *getNewNode( const NODETYPE & ); }; // Default constructor template< class NODETYPE > List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { } // Destructor template< class NODETYPE > List< NODETYPE >::~List() { if ( !isEmpty() ) { // List is not empty cout << "Destroying nodes...\n"; ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes tempPtr = currentPtr; cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; } } cout << "All nodes destroyed\n\n"; } // Insert a node at the front of the list template< class NODETYPE > void List< NODETYPE >::insertAtFront( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { newPtr->nextPtr = firstPtr; firstPtr = newPtr; } } // Insert a node at the back of the list template< class NODETYPE > void List< NODETYPE >::insertAtBack( const NODETYPE &value ) { ListNode< NODETYPE > *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { lastPtr->nextPtr = newPtr; lastPtr = newPtr; } } // Delete a node from the front of the list template< class NODETYPE > bool List< NODETYPE >::removeFromBack( NODETYPE &value ) { if ( isEmpty() ) return return; // delete unsuccessful else { ListNode< NODETYPE > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { ListNode< NODETYPE > *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; } value = tempPtr->data; delete tempPtr; return return; // delete successful } } // Is the List empty? template< class NODETYPE > bool List< NODETYPE >::isEmpty() const { return firstPtr == 0; } // return a pointer to a newly allocated node template< class NODETYPE > ListNode< NODETYPE > *List< NODETYPE >::getNewNode( const NODETYPE &value ) { ListNode< NODETYPE > *ptr = new ListNode< NODETYPE >( value ); assert( ptr != 0 ); return ptr; } // Display the contents of the List template< class NODETYPE > void List< NODETYPE >::print() const { if ( isEmpty() ) { cout << "The list si empty\n\n"; return; } ListNode< NODETYPE > *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) { cout << currentPtr->data << ' '; currentPtr = currentPtr->nextPtr; } cout << "\n\n"; } #endif // queue.h // Queue class template definition // Derived from class List #ifndef QUEUE_H #define QUEUE_H #include "list.h" template< class QUEUETYPE > class Queue: private List< QUEUETYPE > { public: void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); } bool dequeue( QUEUETYPE &d ) { return removeFromFront( d ); } bool isQueueEmpty() const { return isEmpty(); } void printQueue() const { print(); } }; #endif #include <iostream> #include "queue.h" using std::endl; int main() { Queue< int > intQueue; int dequeueInteger, i; cout << "processing an integer Queue" << endl; for ( i = 0; i < 4; i++ ) { intQueue.enqueue( i ); intQueue.printQueue(); } while ( !intQueue.isQueueEmpty() ) { intQueue.dequeue( dequeueInteger ); cout << dequeueInteger << " dequeued" << endl; intQueue.printQueue(); } Queue< double > doubleQueue; double val = 1.1, dequeueDouble; cout << "processing a double Queue" << endl; for ( i = 0; i < 4; i++ ) { doubleQueue.enqueue( val ); doubleQueue.printQueue(); val += 1.1; } while ( !doubleQueue.isQueueEmpty() ) { doubleQueue.dequeue( dequeueDouble ); cout << dequeueDouble << " dequeued" << endl; doubleQueue.printQueue(); } return 0; }
Error:
1. T2.OBJ : error LNK2001: unresolved external symbol "public: bool __thiscall List<int>::removeFromFront(int &)" (?removeFromFront@?$List@H@@QAE_NAAH@Z)
2. T2.OBJ : error LNK2001: unresolved external symbol "public: bool __thiscall List<double>::removeFromFront(double &)" (?removeFromFront@?$List@N@@QAE_NAAN@Z)
3. Debug/T2.exe : fatal error LNK1120: 2 unresolved externals
Honestly speaking i don't have a clue what this means. I tried going through the whole program again and again. Checking through to see what the problem and i can't find it. ( note: T2 is the name of the program. T2.cpp ). Anyone can explain to me on how to handle this?
:mad: Last edited by alc6379; Oct 26th, 2004 at 12:17 pm.
Midnight Skulke:
Just a couple of FYIs when posting on the forums here:
Firstly, I removed the word "urgent" from your thread title. You post is no more urgent than anyone else's here, and it's rude to assume that it is, just because you have a deadline.
Second, we should enforce this for everyone, but it's especially important when you're posting such long code snippets. Please embed your code in Vbulletin code tags. It's just like HTML; just take your code, and put a [ code] tag at the beginning, and a [ /code] tag at the end. (Remove the space after each [ in the tag-- that's the only way I could get the forum to display the tags.
Just a couple of FYIs when posting on the forums here:
Firstly, I removed the word "urgent" from your thread title. You post is no more urgent than anyone else's here, and it's rude to assume that it is, just because you have a deadline.
Second, we should enforce this for everyone, but it's especially important when you're posting such long code snippets. Please embed your code in Vbulletin code tags. It's just like HTML; just take your code, and put a [ code] tag at the beginning, and a [ /code] tag at the end. (Remove the space after each [ in the tag-- that's the only way I could get the forum to display the tags.
Alex Cavnar, aka alc6379
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
Hey thanks for the tips. The second problem is the quicksort and bsearch problem. I tried to declare but i'm not sure how to do it. I tried but everytime I run it, it keep saying undeclared idetifier. So can anyone tell me how to solve this:
[ code]
#include <iostream.h>
#include <algorithm>
#include <stdlib.h>
//class quicksort
class qsort
{
public:
int compare(const void*, const void*);
public:
int list;
int element;
//int compare2;
};
//class bsearch
class bsearch
{
public:
int compare(const void*, const void*);
public:
int list;
int element;
int key;
};
int main()
{
int list[10] = {9,5,2,8,1,7,3,6,4,0};
int key = 3;
//display unsorted list
for (int element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
cout << endl;
qsort (list, 10, sizeof(int), compare1); //error
//display sorted list
for (element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
//displays whether the number in the array or not
if (bsearch(&key,list,10,sizeof(int), compare2)) //error
{
cout<<key<<" is in the array"<<endl; //number in array
}
else
{
cout << key <<" is NOT in the array2" <<endl; //number not in array
}
return 0;
}
int compare3(const void* a_PTR, const void* b_PTR)
{
return *((int*)a_PTR) - *((int*)b_PTR);
}[ /code]
As you can see the code above, where the remark error is it says compare1 and compare2 undeclare identifier. I tried identifying at private and public as int compare1 and int compare2 but it still gives this error. can you give me a example along explanation Sorry but i'm really weak in C++.
[ code]
#include <iostream.h>
#include <algorithm>
#include <stdlib.h>
//class quicksort
class qsort
{
public:
int compare(const void*, const void*);
public:
int list;
int element;
//int compare2;
};
//class bsearch
class bsearch
{
public:
int compare(const void*, const void*);
public:
int list;
int element;
int key;
};
int main()
{
int list[10] = {9,5,2,8,1,7,3,6,4,0};
int key = 3;
//display unsorted list
for (int element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
cout << endl;
qsort (list, 10, sizeof(int), compare1); //error
//display sorted list
for (element = 0; element <= 9; element++)
{
cout << list[element] << " ";
}
//displays whether the number in the array or not
if (bsearch(&key,list,10,sizeof(int), compare2)) //error
{
cout<<key<<" is in the array"<<endl; //number in array
}
else
{
cout << key <<" is NOT in the array2" <<endl; //number not in array
}
return 0;
}
int compare3(const void* a_PTR, const void* b_PTR)
{
return *((int*)a_PTR) - *((int*)b_PTR);
}[ /code]
As you can see the code above, where the remark error is it says compare1 and compare2 undeclare identifier. I tried identifying at private and public as int compare1 and int compare2 but it still gives this error. can you give me a example along explanation Sorry but i'm really weak in C++.
![]() |
Similar Threads
- Please correct the error in my program. When i run the program on turbo c++ 3,. (C++)
- Please find and correct the error in my c++ program. "Linker Error: Undefined symbol (C++)
- Please help me to find error in this program .I am trying to add data to a dynamic ar (C++)
- error in the program (C)
- Linker Error when program is run (Urgent help required Please") (C++)
- Have an error in my program (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with the program i have written i am unable to correct the problems. please
- Next Thread: Need help figuring out C++ Pointers
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






