942,528 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3134
  • C++ RSS
Mar 9th, 2009
0

Linked List - Copy Constructor

Expand Post »
Hey guys,
I'm getting an error at lines #9 and #14. Could someone tell me what I'm doing wrong?

c++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

>>Could someone tell me what I'm doing wrong?
No because you need to post the class and the error message(s)
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5583
Solved Threads: 2279
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,924 posts
since Aug 2005
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

Oops. Here are my errors (same in both spots):

Quote ...
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
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5583
Solved Threads: 2279
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,924 posts
since Aug 2005
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

Here's all of my code, to avoid further confusion. My apologies.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

NodeType is a structure and you have not declared a constructor for it. Fix it like this:
C++ Syntax (Toggle Plain Text)
  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. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5583
Solved Threads: 2279
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,924 posts
since Aug 2005
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

Hm, now I'm getting LINK errors:

Quote ...
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
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Mar 9th, 2009
0

Re: Linked List - Copy Constructor

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].
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5583
Solved Threads: 2279
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,924 posts
since Aug 2005
Mar 10th, 2009
0

Re: Linked List - Copy Constructor

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
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006

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: Wildcard matching algorithm
Next Thread in C++ Forum Timeline: Need help with float function and sales tax code!





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


Follow us on Twitter


© 2011 DaniWeb® LLC