1

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.

7
  • what is the error message? Commented Apr 20, 2015 at 13:29
  • @tobi303 Error 1 error C4703: potentially uninitialized local pointer variable 'node' used Commented Apr 20, 2015 at 13:30
  • This is actually a warning and not an error. What compiler / compiler options are you using? Commented Apr 20, 2015 at 13:32
  • @tobi303 I honestly couldn't tell you. I'm using just the default Visual Studio I guess that is my compiler? Commented Apr 20, 2015 at 13:34
  • There is already an answer, but just in case, you want a fix with less changes of code, you can put something like ListNode* node=NULL when declaring the variable. Commented Apr 20, 2015 at 13:36

2 Answers 2

3

This is probably due to warnings being treated as errors in your build. The compiler is complaining that node might not be initialized when node->link is accessed. However, that won't actually be the case because if snode is null, the while block won't be accessed, so you won't access the uninitialized memory. If you want to make the warning go away, putting the while loop inside the if block will probably work:

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_;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much it now works! I have one more question and I edited into my question if you could look at it it would be much appreciated!
You'd probably be better off writing a new question detailing your new issue and what you've tried to solve it.
0

This is a false warning as the compiler seems to think you are going to use the node without having assigned it (because it might skip the if statement). In this scenario I do not see how this would happen as the while (snode) would never be entered.

You could simply ignore this warning (disable treat warnings as errors in the project settings) or remove this warning from this specific part of code (#pragma warning(disable : 4703)).

To build without treating warnings as errors go to: Project -> Properties -> C/C++ -> Treat warnings as errors. Disable this option or use the pragma directives.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.