I am really new to C++ and this code was an example in my book so it should work because I have to implement a few new functions into this. However, I copied the code line for line and I keep getting this error message now my code won't compile until I fix it.
It says that my local pointer 'node' is being used. I don't know what this actually means. Could anyone tell me whats is the error actually telling me? Also could some one help me fix this so I can start my project? I'm only asking for the code because this isn't part of my project, it was already given by the teacher.
Here is my code:
// ListNode.h #ifndef _LISTNODE_H #define _LISTNODE_H #include <cstdlib> typedef int ItemType; class ListNode { friend class LList; public: ListNode(ItemType item, ListNode* link = NULL); private: ItemType item_; ListNode *link_; }; inline ListNode::ListNode(ItemType item, ListNode *link) { item_ = item; link_ = link; } #endif // _LISTNODE_H // LList.h #ifndef _LLIST_H #define _LLIST_H #include "ListNode.h" class LList { public: LList(); LList(const LList& source); ~LList(); LList& operator=(const LList& source); int size() { return size_; } void append(ItemType x); void insert(size_t i, ItemType x); ItemType pop(int i = -1); ItemType& operator[](size_t position); private: // methods void copy(const LList &source); void dealloc(); ListNode* _find(size_t position); ItemType _delete(size_t position); // data elements ListNode *head_; int size_; }; #endif // _LLIST_H // LList.cpp #include "LList.h" LList::LList() { head_ = NULL; size_ = 0; } ListNode* LList::_find(size_t position) { ListNode *node = head_; size_t i; for (i = 0; i<position; i++) { node = node->link_; } return node; } ItemType LList::_delete(size_t position) { ListNode *node, *dnode; ItemType item; if (position == 0) { dnode = head_; head_ = head_->link_; item = dnode->item_; delete dnode; } else { node = _find(position - 1); if (node != NULL) { dnode = node->link_; node->link_ = dnode->link_; item = dnode->item_; delete dnode; } } size_ -= 1; return item; } void LList::append(ItemType x) { ListNode *node, *newNode = new ListNode(x); if (head_ != NULL) { node = _find(size_ - 1); node->link_ = newNode; } else { head_ = newNode; } size_ += 1; } void LList::insert(size_t i, ItemType x) { ListNode *node; if (i == 0) { head_ = new ListNode(x, head_); } else { node = _find(i - 1); node->link_ = new ListNode(x, node->link_); } size_ += 1; } ItemType LList::pop(int i) { if (i == -1) { i = size_ - 1; } return _delete(i); } ItemType& LList::operator[](size_t position) { ListNode *node; node = _find(position); return node->item_; } LList::LList(const LList& source) { copy(source); } void LList::copy(const LList &source) { ListNode *snode, *node; snode = source.head_; if (snode) { node = head_ = new ListNode(snode->item_); snode = snode->link_; } while (snode) { node->link_ = new ListNode(snode->item_); node = node->link_; snode = snode->link_; } size_ = source.size_; } LList& LList::operator=(const LList& source) { if (this != &source) { dealloc(); copy(source); } return *this; } LList::~LList() { dealloc(); } void LList::dealloc() { ListNode *node, *dnode; node = head_; while (node) { dnode = node; node = node->link_; delete dnode; } } I know where the problem is exactly (Line 104 in my code)
node->link_ = new ListNode(snode->item_); This part of my code is the problem. Could anyone help me fix this problem so I can work on my program? Thanks!
Now that my previous problem was answered I have a new one.
How would I go about testing my code? I have a few lines but it keeps coming out with errors when I try to print out the contents of my LList. This is my test code:
#include "LList.h" int main() { LList b, c; int x; b.append(1); b.append(2); b.append(3); c.append(4); c.append(5); c = b; x = b.pop(); } Could anyone help me write a working test code, this the last thing I will need to start adding my different functions.