943,106 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 943
  • C++ RSS
Dec 30th, 2009
0

C++ Doubly Linked List Template Error Help

Expand Post »
I am attempting to program a doubly linked list template. My program is setup with the class node defined in a header file,the list itself in a header file and finally the main in a separate cpp file which calls the list header. The list header calls the node header. The error I am receiving is
"dbList<int>::dbList(int, Node<int>*, Node<int>*)", referenced from:
_main in dbList.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Here is the code below:
c++ Syntax (Toggle Plain Text)
  1. template<class dtype>
  2. class Node
  3. {
  4. public:
  5. int position;
  6. dtype data;
  7. Node<dtype>* next;
  8. Node<dtype>* prev;
  9. Node( dtype data, Node<dtype>* next=0, Node<dtype>* prev=0);
  10. };

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include "dbNode.h"
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. template <typename Type>
  7. class dbList
  8. {
  9. private:
  10. int size;
  11. Node<Type>* head;
  12. Node<Type>* tail;
  13.  
  14. public:
  15. dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0);
  16. void insertNode(Type v);
  17. void insertNode(Type v, int pos);
  18. void displayList();
  19. void tester();
  20. };
  21.  
  22. template <typename Type>
  23. void dbList<Type>::insertNode(Type v)
  24. {
  25. Node<Type>* tempa;
  26. Node<Type>* tempb;
  27. Node<Type>* tempc;
  28. tempa->data=v;
  29.  
  30. if(this->head==0)
  31. {
  32. this ->head = tempa;
  33. this ->head ->next = 0;
  34. this ->head ->prev = 0;
  35. this ->size++;
  36. this ->head->position=size;
  37. }
  38.  
  39. else
  40. {
  41. tempb = this ->head;
  42. if(tempb ->next == 0)
  43. {
  44. tempa->next = tempb->next;
  45. tempa ->prev = tempb;
  46. tempb ->next = tempa;
  47. this ->tail = tempa;
  48. this ->size++;
  49. this ->tail->position=size;
  50. }
  51.  
  52. else
  53. {
  54. tempc = this ->tail;
  55. tempa ->next = tempc ->next;
  56. tempa ->prev = tempc;
  57. tempc ->next = tempa;
  58. this ->tail = tempa;
  59. this ->size++;
  60. tempc->position=size;
  61. }
  62.  
  63. }
  64. }
  65. template<typename Type>
  66. void dbList<Type>::displayList()
  67. {
  68. Node<Type> *tempa;
  69. tempa = this ->head;
  70. if(tempa == 0)
  71. {
  72. cout<<"This list is empty";
  73. }
  74. else
  75. {
  76. cout<<"List- ";
  77. while(tempa != 0)
  78. {
  79. cout<< tempa -> data;
  80. cout<< " ";
  81. tempa = tempa -> next;
  82. }
  83. cout<< "\n";
  84. }
  85. }
  86. template<typename Type>
  87. void dbList<Type>::tester()
  88. {
  89. int selection;
  90. Type tempa;
  91. cout << "What would you like to do\n";
  92. cout << "1. Insert Item\n"<<"2.Print List\n";
  93. cin>>selection;
  94. cout<<"\n";
  95. switch(selection)
  96. {
  97. case 1:cout<<"What value would you like to add?";
  98. cin>>tempa;
  99. cout<<"\n";
  100. insertNode(tempa);
  101. break;
  102.  
  103. case 2:dbList<Type>::displayList();
  104. break;
  105. }
  106. }

C++ Syntax (Toggle Plain Text)
  1. #include"dbList.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main ()
  8. {
  9. dbList<int> intlist;
  10. intlist.tester();
  11.  
  12. }

This is my first time using templates. Please point out anything that does not make sense at all. I've been going through "C++ Templates the complete guide" , but it really is not very clear to me in some spots. So any help would be greatly appreciated. Thank you for your time.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rgpii is offline Offline
22 posts
since Oct 2009
Dec 30th, 2009
0
Re: C++ Doubly Linked List Template Error Help
C++ Syntax (Toggle Plain Text)
  1. dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0);

it seems that you only declare that method. you have to implement it.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Dec 30th, 2009
0
Re: C++ Doubly Linked List Template Error Help
That was supposed to be the constructor. It was the syntax I saw other templates using. Is that incorrect?

I just changed
c++ Syntax (Toggle Plain Text)
  1. dbList(int size=0,Node<Type>* head=0,Node<Type>* tail=0);

to

C++ Syntax (Toggle Plain Text)
  1. dbList()
  2. {
  3. int size=0;
  4. Node<Type>* head=0;
  5. Node<Type>* tail=0;
  6. }

It compiled but those variables were unused and the tester method sayd Bus Error when I try to input values. Any thoughts? I mean obviously the constructor is not working in the traditional form like I just used because the variables arent being implemented at all
Last edited by rgpii; Dec 30th, 2009 at 2:21 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rgpii is offline Offline
22 posts
since Oct 2009
Dec 30th, 2009
0
Re: C++ Doubly Linked List Template Error Help
c++ Syntax (Toggle Plain Text)
  1. template <typename Type>
  2. void dbList<Type>::insertNode(Type v)
  3. {
  4. Node<Type>* tempa;
  5. Node<Type>* tempb;
  6. Node<Type>* tempc;
  7. tempa->data=v;
  8.  
  9. if(this->head==0)
  10. {
  11. this ->head = tempa;
  12. this ->head ->next = 0;
  13. this ->head ->prev = 0;
  14. this ->size++;
  15. this ->head->position=size;
  16. }
  17.  
  18. else
  19. {
  20. tempb = this ->head;
  21. if(tempb ->next == 0)
  22. {
  23. tempa->next = tempb->next;
  24. tempa ->prev = tempb;
  25. tempb ->next = tempa;
  26. this ->tail = tempa;
  27. this ->size++;
  28. this ->tail->position=size;
  29. }
  30.  
  31. else
  32. {
  33. tempc = this ->tail;
  34. tempa ->next = tempc ->next;
  35. tempa ->prev = tempc;
  36. tempc ->next = tempa;
  37. this ->tail = tempa;
  38. this ->size++;
  39. tempc->position=size;
  40. }
  41.  
  42. }
  43. }

line 7: should be the first segmentation fault since 'tempa' is not pointing to anything and you try to deference it and assign value to 'data'

line11: this->head = tempa is also wrong, tempa is nothing, and you are assigning this->head to nothing, when you call display it'll probably crash again.

Probable solution:
Remove line 7.
Before you assign 'tempa' to this->head, create a new node and point tempa to it and then assign value to 'data'

something like
c++ Syntax (Toggle Plain Text)
  1. temp = new Node<int>();
Last edited by Agni; Dec 30th, 2009 at 4:02 pm.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 30th, 2009
0
Re: C++ Doubly Linked List Template Error Help
First, thank you for all of the replies. Those combined with the generic doubly linked list code snippet has actually allowed my code to compile. I am now receiving a bus error and I am at a complete loss as to why this is happening. Here is my new code.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. template <typename Type>
  5. class dbList
  6. {
  7. public:
  8. struct Node
  9. {
  10. Type data;
  11. Type position;
  12. Node* prev;
  13. Node* next;
  14. Node(Type t, Node* p, Node* n) : data(t), prev(p), next(n) {}//Could you please explain the purpose this line here? Why am I initializing prev and next to p and n
  15. };
  16.  
  17. dbList(): size(0), head(0), tail (0) {}
  18. void insertNode(Type v);
  19. void insertNode(Type v, int pos);
  20. void displayList();
  21. void tester();
  22.  
  23. private:
  24.  
  25. int size;
  26. Node* head;
  27. Node* tail;
  28.  
  29. };
  30.  
  31. template <typename Type>
  32. void dbList<Type>::insertNode(Type v)
  33. {
  34. Node* tempa;
  35. Node* tempb;
  36. Node* tempc;
  37. tempa->data=v;
  38.  
  39. if(this->head==0)
  40. {
  41. this ->head = tempa;
  42. this ->head ->next = 0;
  43. this ->head ->prev = 0;
  44. this ->size++;
  45. this ->head->position=size;
  46. }
  47.  
  48. else
  49. {
  50. tempb = this ->head;
  51. if(tempb ->next == 0)
  52. {
  53. tempa->next = tempb->next;
  54. tempa ->prev = tempb;
  55. tempb ->next = tempa;
  56. this ->tail = tempa;
  57. this ->size++;
  58. this ->tail->position=size;
  59. }
  60.  
  61. else
  62. {
  63. tempc = this ->tail;
  64. tempa ->next = tempc ->next;
  65. tempa ->prev = tempc;
  66. tempc ->next = tempa;
  67. this ->tail = tempa;
  68. this ->size++;
  69. tempc->position=size;
  70. }
  71.  
  72. }
  73. }
  74. template<typename Type>
  75. void dbList<Type>::displayList()
  76. {
  77. Node *tempa;
  78. tempa = this ->head;
  79. if(tempa == 0)
  80. {
  81. cout<<"This list is empty";
  82. }
  83. else
  84. {
  85. cout<<"List- ";
  86. while(tempa != 0)
  87. {
  88. cout<< tempa -> data;
  89. cout<< " ";
  90. tempa = tempa -> next;
  91. }
  92. cout<< "\n";
  93. }
  94. }
  95. template<typename Type>
  96. void dbList<Type>::tester()
  97. {
  98. int tester=0;
  99. int selection;
  100. Type tempa;
  101. while(tester==0)
  102. {
  103.  
  104. cout << "What would you like to do\n";
  105. cout << "9. Exit\n" << "1. Insert Item\n"<<"2.Print List\n";
  106. cin>>selection;
  107. cout<<"\n";
  108.  
  109.  
  110. switch(selection)
  111. {
  112. case 1:cout<<"What value would you like to add?";
  113. cout<< "\n";
  114. cin>>tempa;
  115. cout<<"\n";
  116. insertNode(tempa);
  117. break;
  118.  
  119. case 2:dbList<Type>::displayList();
  120. break;
  121. case 3:tester=1;
  122. break;
  123. }
  124. }
  125. }

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include"dbList.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. int main ()
  9. {
  10. dbList<int> intlist;
  11. intlist.tester();
  12.  
  13. }

I am recieving a bus error after I try to input a second value into a new node.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rgpii is offline Offline
22 posts
since Oct 2009
Dec 31st, 2009
0
Re: C++ Doubly Linked List Template Error Help
template <typename Type>
void dbList<Type>::insertNode(Type v)
{
   Node* tempa;
   Node* tempb;
   Node* tempc;
   tempa->data=v;
Where does tempa point?
Last edited by Dave Sinkula; Dec 31st, 2009 at 12:57 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 31st, 2009
0
Re: C++ Doubly Linked List Template Error Help
rgpii i don't think you are reading any of the anwers carefully. I see the same mistakes in your code again, which I pointed out in my post earlier. If you don't read and apply what' been suggested then there's really no point.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: operator << >>
Next Thread in C++ Forum Timeline: help me plz :S





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC