| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help with my code
hi there
can anyone help me with my code? it can be compiled successfully in vc++6.0, however it fails to run. here's the code: //set.h #include <iostream> using namespace std; struct Node{ char element; Node* next; }; class Set { public: Set(); Set(char start, char end); void printSet(); private: Node* head; }; //set.cpp #include <iostream> #include "set.h" Set::Set() { head = NULL; } Set::Set(char start, char end) { char temp = start; Node* ptemp = new Node; ptemp->element = temp; ptemp->next = NULL; head = ptemp; temp++; while (temp != end){ ptemp = ptemp->next; ptemp->element = temp; temp++; } } void Set::printSet(){ cout << "["; if (head != NULL){ Node* pprint = head; cout << pprint->element; pprint = pprint->next; while (pprint != NULL) { cout << ',' << pprint->element; pprint = pprint->next; } } cout << "]"; } //main.cpp #include "set.h" using namespace std; void main() { Set B('A', 'Z'); B.printSet(); } //basically i want to store A~Z in a linked list and print it out, thanks |
|
#2
|
|||
|
|||
|
One thing I can see is to not to inclde header file iostream.h again in your set.cpp file since it will be called automatiacally by set.h.
See def. of "set.h" . Either remove iostream.h from set.cpp or remove its declaration in the set.h |
|
#3
|
||||
|
||||
|
Including iostream multiple times will not hurt your program, it will only be included once.
In your constructor you try to create a list from start to end right? (consider the class list from the stl library by the way..). In the while loop in your constructor you are not creating new nodes. So when you do ptemp = ptemp->next ptemp->next is NULL, so ptemp is now NULL. ptemp->element = temp will now break because ptemp is a NULL pointer. So you need to add ptemp->next = new Node; Before ptemp = ptemp->next You also have an off-by-one bug in the while loop, the end character will not be added. Good luck! Last edited by Icon : February 19th, 2006 at 08:29 AM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Need help with my code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|