| | |
Linked List - Copy Constructor
![]() |
Hey guys,
I'm getting an error at lines #9 and #14. Could someone tell me what I'm doing wrong?
I'm getting an error at lines #9 and #14. Could someone tell me what I'm doing wrong?
c++ Syntax (Toggle Plain Text)
StackType::StackType(const StackType & st) // copy c-tor { if ( st.topPtr == NULL ) topPtr = NULL; else { NodeType * stptr; // pointer to source Stack NodeType * p; // pointer to target Stack topPtr = new NodeType(st.topPtr->info); // first node stptr = st.topPtr->next; // point to second Node p = topPtr; // initialize pointer to Stack copy while ( stptr != NULL ) // deep copy other nodes { p->next = new NodeType(stptr->info); p = p->next; // update pointers stptr = stptr->next; } // endwhile p->next = NULL; } // endif }
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
-Edsger Dijkstra
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
-Edsger Dijkstra
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
Weight Lost since 1 Jan 2010: 8.7 lbs
Here's all of my code, to avoid further confusion. My apologies.
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?
c++ Syntax (Toggle Plain Text)
#pragma once #include <iostream> using namespace std ; typedef char ItemType; struct NodeType { ItemType info ; NodeType* next ; }; class FullStack // Exception class thrown by Push when stack is full. {}; class EmptyStack // Exception class thrown by Pop and Top when stack is emtpy. {}; class StackType { public: StackType(); StackType(const StackType & copy) ; ~StackType(); void Push(ItemType); void Pop(); ItemType Top(); bool IsEmpty() const; bool IsFull() const; bool Identical(StackType stack) ; private: NodeType* topPtr; int length ; }; #include "StackType.h" // Implementation file for linked StackType void StackType::Push(ItemType newItem) // Adds newItem to the top of the stack. // Stack is bounded by size of memory. // Pre: Stack has been initialized. // Post: If stack is full, FullStack exception is thrown; // else newItem is at the top of the stack. { if (IsFull()) throw FullStack(); else { NodeType* location; location = new NodeType; location->info = newItem; location->next = topPtr; topPtr = location; length++ ; } } void StackType::Pop() // Removes top item from Stack and returns it in item. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else top element has been removed. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; length++ ; } } ItemType StackType::Top() // Returns a copy of the top item in the stack. // Pre: Stack has been initialized. // Post: If stack is empty, EmptyStack exception is thrown; // else a copy of the top element is returned. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; } StackType::StackType() // Class constructor. { topPtr = NULL; length = 0 ; } StackType::StackType(const StackType & st) // copy c-tor { if ( st.topPtr == NULL ) topPtr = NULL; else { NodeType * stptr; // pointer to source Stack NodeType * p; // pointer to target Stack topPtr = new NodeType(st.topPtr->info); // first node stptr = st.topPtr->next; // point to second Node p = topPtr; // initialize pointer to Stack copy while ( stptr != NULL ) // deep copy other nodes { p->next = new NodeType(stptr->info); p = p->next; // update pointers stptr = stptr->next; } // endwhile p->next = NULL; } // endif } bool StackType::IsFull() const // Returns true if there is no room for another ItemType // on the free store; false otherwise. { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; } } bool StackType::IsEmpty() const { return (topPtr == NULL) ; } StackType::~StackType() // Post: stack is empty; all items have been deallocated. { NodeType* tempPtr; while (topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } } bool StackType::Identical(StackType stack) { NodeType* tmp = stack.topPtr ; NodeType* tmp2 = topPtr ; bool iden = true ; if (length != stack.length) iden = false ; while(tmp->next != NULL && tmp2->next != NULL && iden) { if (tmp->info == tmp2->info) { iden = true ; tmp = tmp->next ; tmp2 = tmp2->next ; } else iden = false ; } return iden ; } #include "StackType.h" int main() { ItemType a = 'a' ; ItemType b = 'b' ; ItemType c = 'c' ; ItemType d = 'd' ; StackType stack ; stack.Push(a) ; stack.Push(b) ; stack.Push(c) ; stack.Push(d) ; StackType stack_two ; stack_two.Push(a) ; stack_two.Push(b) ; stack_two.Push(c) ; stack_two.Push(d) ; bool iden = stack.Identical(stack_two) ; if (iden) cout << "Identical!\n" ; else cout << "Not Identical!\n" ; cout << "\n-- Program Complete --\n" ; return 0 ; }
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
-Edsger Dijkstra
NodeType is a structure and you have not declared a constructor for it. Fix it like this:
C++ Syntax (Toggle Plain Text)
struct NodeType { ItemType info ; NodeType* next ; NodeType(ItemType t) {info = t; next = NULL;} NodeType() {info = 0; next = NULL;} };
My NewYear's resolution is to lose weight.
Weight Lost since 1 Jan 2010: 8.7 lbs
Weight Lost since 1 Jan 2010: 8.7 lbs
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
-Edsger Dijkstra
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
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
-Edsger Dijkstra
![]() |
Similar Threads
- Help with getting multiple copies(copy constructor help) (C++)
- ordered Linked List Copy function (C++)
- Linked Lists and Copy Constructor (C++)
- This Pointer / Copy Constructor (C++)
- Sorted linked list help (C++)
- (C++) Writing a Copy Constructor?!? (C++)
- recursive linked list (C++)
- Checking for duplicates in a orderedered linked list (C++)
- Linked List (C++)
Other Threads in the C++ Forum
- Previous Thread: Wildcard matching algorithm
- Next Thread: Need help with float function and sales tax code!
Views: 1912 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ vector video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






