Linked List - Copy Constructor

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Reply

Join Date: Jun 2006
Posts: 1,171
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Linked List - Copy Constructor

 
0
  #1
Mar 9th, 2009
Hey guys,
I'm getting an error at lines #9 and #14. Could someone tell me what I'm doing wrong?

  1. StackType::StackType(const StackType & st) // copy c-tor
  2. {
  3. if ( st.topPtr == NULL )
  4. topPtr = NULL;
  5. else
  6. {
  7. NodeType * stptr; // pointer to source Stack
  8. NodeType * p; // pointer to target Stack
  9. topPtr = new NodeType(st.topPtr->info); // first node
  10. stptr = st.topPtr->next; // point to second Node
  11. p = topPtr; // initialize pointer to Stack copy
  12. while ( stptr != NULL ) // deep copy other nodes
  13. {
  14. p->next = new NodeType(stptr->info);
  15. p = p->next; // update pointers
  16. stptr = stptr->next;
  17. } // endwhile
  18. p->next = NULL;
  19. } // endif
  20. }
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,166
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Linked List - Copy Constructor

 
0
  #2
Mar 9th, 2009
>>Could someone tell me what I'm doing wrong?
No because you need to post the class and the error message(s)
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,171
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Linked List - Copy Constructor

 
0
  #3
Mar 9th, 2009
Oops. Here are my errors (same in both spots):

Error 1 error C2664: 'NodeType::NodeType(const NodeType &)' : cannot convert parameter 1 from 'ItemType' to 'const NodeType &' j:\Projects\ch5 identical linked stack\ch5 identical linked stack\StackType.cpp 66 ch5 identical linked stack
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,166
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Linked List - Copy Constructor

 
0
  #4
Mar 9th, 2009
again you have to post the class declaration. But my guess from that error message is that the parameter in the function prototype is not the same as in the function implementation. But the only way to tell is for you to post the class.
Last edited by Ancient Dragon; Mar 9th, 2009 at 3:03 pm.
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,171
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Linked List - Copy Constructor

 
0
  #5
Mar 9th, 2009
Here's all of my code, to avoid further confusion. My apologies.

  1. #pragma once
  2. #include <iostream>
  3. using namespace std ;
  4.  
  5. typedef char ItemType;
  6. struct NodeType
  7. {
  8. ItemType info ;
  9. NodeType* next ;
  10. };
  11.  
  12. class FullStack
  13. // Exception class thrown by Push when stack is full.
  14. {};
  15.  
  16. class EmptyStack
  17. // Exception class thrown by Pop and Top when stack is emtpy.
  18. {};
  19.  
  20. class StackType
  21. {
  22. public:
  23. StackType();
  24. StackType(const StackType & copy) ;
  25. ~StackType();
  26. void Push(ItemType);
  27. void Pop();
  28. ItemType Top();
  29. bool IsEmpty() const;
  30. bool IsFull() const;
  31. bool Identical(StackType stack) ;
  32. private:
  33. NodeType* topPtr;
  34. int length ;
  35. };
  36.  
  37.  
  38.  
  39. #include "StackType.h"
  40.  
  41. // Implementation file for linked StackType
  42. void StackType::Push(ItemType newItem)
  43. // Adds newItem to the top of the stack.
  44. // Stack is bounded by size of memory.
  45. // Pre: Stack has been initialized.
  46. // Post: If stack is full, FullStack exception is thrown;
  47. // else newItem is at the top of the stack.
  48.  
  49. {
  50. if (IsFull())
  51. throw FullStack();
  52. else
  53. {
  54. NodeType* location;
  55. location = new NodeType;
  56. location->info = newItem;
  57. location->next = topPtr;
  58. topPtr = location;
  59. length++ ;
  60. }
  61. }
  62. void StackType::Pop()
  63. // Removes top item from Stack and returns it in item.
  64. // Pre: Stack has been initialized.
  65. // Post: If stack is empty, EmptyStack exception is thrown;
  66. // else top element has been removed.
  67. {
  68. if (IsEmpty())
  69. throw EmptyStack();
  70. else
  71. {
  72. NodeType* tempPtr;
  73. tempPtr = topPtr;
  74. topPtr = topPtr->next;
  75. delete tempPtr;
  76. length++ ;
  77. }
  78. }
  79. ItemType StackType::Top()
  80. // Returns a copy of the top item in the stack.
  81. // Pre: Stack has been initialized.
  82. // Post: If stack is empty, EmptyStack exception is thrown;
  83. // else a copy of the top element is returned.
  84. {
  85. if (IsEmpty())
  86. throw EmptyStack();
  87. else
  88. return topPtr->info;
  89. }
  90. StackType::StackType() // Class constructor.
  91. {
  92. topPtr = NULL;
  93. length = 0 ;
  94. }
  95.  
  96. StackType::StackType(const StackType & st) // copy c-tor
  97. {
  98. if ( st.topPtr == NULL )
  99. topPtr = NULL;
  100. else
  101. {
  102. NodeType * stptr; // pointer to source Stack
  103. NodeType * p; // pointer to target Stack
  104. topPtr = new NodeType(st.topPtr->info); // first node
  105. stptr = st.topPtr->next; // point to second Node
  106. p = topPtr; // initialize pointer to Stack copy
  107. while ( stptr != NULL ) // deep copy other nodes
  108. {
  109. p->next = new NodeType(stptr->info);
  110. p = p->next; // update pointers
  111. stptr = stptr->next;
  112. } // endwhile
  113. p->next = NULL;
  114. } // endif
  115. }
  116.  
  117. bool StackType::IsFull() const
  118. // Returns true if there is no room for another ItemType
  119. // on the free store; false otherwise.
  120. {
  121. NodeType* location;
  122. try
  123. {
  124. location = new NodeType;
  125. delete location;
  126. return false;
  127. }
  128. catch(std::bad_alloc exception)
  129. {
  130. return true;
  131. }
  132. }
  133.  
  134. bool StackType::IsEmpty() const
  135. {
  136. return (topPtr == NULL) ;
  137. }
  138.  
  139. StackType::~StackType()
  140. // Post: stack is empty; all items have been deallocated.
  141. {
  142. NodeType* tempPtr;
  143.  
  144. while (topPtr != NULL)
  145. {
  146. tempPtr = topPtr;
  147. topPtr = topPtr->next;
  148. delete tempPtr;
  149. }
  150. }
  151.  
  152. bool StackType::Identical(StackType stack)
  153. {
  154. NodeType* tmp = stack.topPtr ;
  155. NodeType* tmp2 = topPtr ;
  156. bool iden = true ;
  157.  
  158. if (length != stack.length)
  159. iden = false ;
  160.  
  161. while(tmp->next != NULL && tmp2->next != NULL && iden)
  162. {
  163. if (tmp->info == tmp2->info)
  164. {
  165. iden = true ;
  166. tmp = tmp->next ;
  167. tmp2 = tmp2->next ;
  168. }
  169. else
  170. iden = false ;
  171. }
  172.  
  173. return iden ;
  174. }
  175.  
  176.  
  177. #include "StackType.h"
  178.  
  179. int main()
  180. {
  181. ItemType a = 'a' ;
  182. ItemType b = 'b' ;
  183. ItemType c = 'c' ;
  184. ItemType d = 'd' ;
  185.  
  186. StackType stack ;
  187. stack.Push(a) ;
  188. stack.Push(b) ;
  189. stack.Push(c) ;
  190. stack.Push(d) ;
  191.  
  192. StackType stack_two ;
  193. stack_two.Push(a) ;
  194. stack_two.Push(b) ;
  195. stack_two.Push(c) ;
  196. stack_two.Push(d) ;
  197.  
  198. bool iden = stack.Identical(stack_two) ;
  199. if (iden)
  200. cout << "Identical!\n" ;
  201. else
  202. cout << "Not Identical!\n" ;
  203.  
  204. cout << "\n-- Program Complete --\n" ;
  205.  
  206. return 0 ;
  207. }

The only reason I'm even trying to do the copy c-tor is because after the program runs, it gives me a run-time error at the very end when it tries to destruct the already destructed memory. I figure this is the only way around that?
Last edited by Duki; Mar 9th, 2009 at 3:07 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,166
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Linked List - Copy Constructor

 
0
  #6
Mar 9th, 2009
NodeType is a structure and you have not declared a constructor for it. Fix it like this:
  1. struct NodeType
  2. {
  3. ItemType info ;
  4. NodeType* next ;
  5. NodeType(ItemType t) {info = t; next = NULL;}
  6. NodeType() {info = 0; next = NULL;}
  7. };
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,171
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Linked List - Copy Constructor

 
0
  #7
Mar 9th, 2009
Hm, now I'm getting LINK errors:

Error 2 error LNK2020: unresolved token (0A0002C1) "public: bool __thiscall StackType::Identical(class StackType)" (?Identical@StackType@@$$FQAE_NV1@@Z) main.obj ch5 identical linked stack


Error 1 error LNK2034: metadata inconsistent with COFF symbol table: symbol '?Identical@StackType@@$$FQAE_NV1@@Z' (06000043) has inconsistent metadata with (0A0002C1) in main.obj StackType.obj ch5 identical linked stack


Error 3 fatal error LNK1120: 1 unresolved externals J:\Projects\ch5 identical linked stack\Debug\ch5 identical linked stack.exe 1 ch5 identical linked stack
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,166
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Linked List - Copy Constructor

 
0
  #8
Mar 9th, 2009
must be some other problem because I didn't get any link errors when I put all that code into one *.cpp file and commented out the [icode]#include "StackType.h"[/code].
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,171
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: Linked List - Copy Constructor

 
0
  #9
Mar 10th, 2009
Might have something to do with the fact I was running it off of my flash drive. I'll give it a whirl again tomorrow.

Thanks for the help
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1912 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC