可能使用了未初始化的本地指针变量“节点". C ++ [英] Potentially uninitialized local pointer variable 'node' used. C++

查看:254
本文介绍了可能使用了未初始化的本地指针变量“节点". C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是C ++的新手,此代码是我书中的一个示例,因此它应该起作用,因为我必须在其中实现一些新功能.但是,我将代码行复制为line,并且一直收到此错误消息,但是直到修复该错误,我的代码才可以编译.

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.

这是我的代码:

// 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;
    }
}

我知道问题出在哪里(代码中的第104行)

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.

我将如何测试我的代码?我有几行,但是当我尝试打印LList的内容时,它总是出现错误.这是我的测试代码:

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.

推荐答案

这可能是由于警告被视为构建中的错误.编译器抱怨在访问node->link时可能未初始化node.但是,实际上并非如此,因为如果snode为null,则将不会访问while块,因此您将不会访问未初始化的内存.如果您想使警告消失,可以将while循环放在if块中可能会起作用:

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_;
    }
}

这篇关于可能使用了未初始化的本地指针变量“节点". C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆