C++ 链表:Can';t访问最后一个节点,获取R6010(C+;+;,VS2010)

C++ 链表:Can';t访问最后一个节点,获取R6010(C+;+;,VS2010),c++,visual-studio-2010,C++,Visual Studio 2010,我有一个模板链接列表类。当我添加五个元素时,“size”数据成员说有五个元素,但当我使用“getAt()”函数时,我似乎无法访问第五个节点。它给了我R6010错误(要求中止)。我似乎找不到getAt()函数中的逻辑问题。也许它实际上并没有添加第五个?不过,我不这么认为。以前从未见过这种错误 //LinkedList.h #include <iostream> using namespace std; /* class: Node description: holds an T i

我有一个模板链接列表类。当我添加五个元素时,“size”数据成员说有五个元素,但当我使用“getAt()”函数时,我似乎无法访问第五个节点。它给了我R6010错误(要求中止)。我似乎找不到getAt()函数中的逻辑问题。也许它实际上并没有添加第五个?不过,我不这么认为。以前从未见过这种错误

//LinkedList.h
#include <iostream>
using namespace std;


/*
class: Node
description: holds an T in 'info' and a Node pointer in
             'next'. the building block of a linked list.
*/
template <typename T>
class Node{
public:
    T info;//holds the info value
    Node<T>* next;//holds a pointer to the next node in the list

    Node(T val);//constructor
};

/*  
function: constructor
param(s): T (value)
pre:
post: instantiates node, sets "next" pointer to NULL
exception(s):
return:
*/
template <typename T>
Node<T>::Node(T val){//constructor, accepts a value to hold the info
    info = val;
    next = NULL;
}//end Node class


///////////////////////////////////////////////
///////////////////////////////////////////////


/*
class: LinkedList
description: a list of linked T-type nodes. provides methods to add
             node and retrieve a node at a given location in the list. 
*/
template <typename T>
class LinkedList{
public:
    Node<T>* list;//points to the first node of the list
    int size;//the number of nodes in the list

    LinkedList();//default constructor
    ~LinkedList();//destructor
    void add(T addArg);//add a node
    T getAt(int getArg);//get a node at a position 'getArg'
    void updateAt(int getArg, T newData);
};//end LinkedList class

/*  
function: linked list default constructor
param(s): 
pre: 
post: list is instantiated, size is set to 0
exception(s): 
return: 
*/
template <typename T>
LinkedList<T>::LinkedList(){
    list = NULL;
    size = 0;
}

/*  
function: linked list destructor
param(s):
pre:
post: all nodes and pointer to the first node deleted
exception(s):
return:
*/
template <typename T>
LinkedList<T>::~LinkedList(){
    while(list != NULL){
        Node<T>* temp = list->next;
        delete list;
        list = temp;
    }
}

/*  
function: add
param(s): T (addArg)
pre: list is instantiated
post: new node has been added to the node, link of previous node
      has been set to the new node. if no nodes in list before
      adding, link of the new node is NULL
exception(s): 
return: void
*/
template <typename T>
void LinkedList<T>::add(T addArg){
    if(size == 0){//if the list is empty
        Node<T>* next = new Node<T>(addArg);//create a new node
        list = next;//and set the 'list' pointer to it
        size++;//increment size of list
    }
    else if(size > 0){//if there's at least one node in the list
        Node<T>* temp = list;//create new node 
        while(temp->next != NULL){//traverse list to last node
            temp = temp->next;
        }
        temp->next = new Node<T>(addArg);//set the link of the last
                                      //node to a new node of value
                                      //addArg
        size++;//increment size of the list
    }
    else//throw exception, the list has a negative size value
        throw string("Size is negative for some reason...\n");
}

/*  
function: getAt
param(s): getArg(int, the position of the node to retrieve)
pre: list isn't empty
post: 
exception(s): throw out of bounds exception of getArg is negative 
              or out of bounds
return: value of the node at position 'getArg'
*/
template <typename T>
T LinkedList<T>::getAt(int getArg){
    if((getArg>=size)||(getArg<0))//getArg out of bounds
        throw string("Out of bounds 'get' argument");
    else{//getArg is acceptable
        Node<T>* temp = list;//create a temp pointer so as to not lose
                          // the pointer to the list
        for(int i = 1; i < getArg; i++){//traverse list until the
            temp = temp->next;          //sought-after node is found
        }
        return temp->info;//return the value of the sought-after node
    }
}

/*  
function: updateAt
param(s): getArg(int, the position of the node to retrieve)
          newData(T, new data)
pre: list isn't empty
post: info at node getArg is changed to 
exception(s): throw out of bounds exception of getArg is negative 
              or out of bounds
return: value of the node at position 'getArg'
*/
template <typename T>
void LinkedList<T>::updateAt(int getArg, T newData){
    if((getArg>=size)||(getArg<0))//getArg out of bounds
        throw string("Out of bounds 'get' argument");
    else{//getArg is acceptable
        Node<T>* temp = list;//create a temp pointer so as to not lose
                          // the pointer to the list
        for(int j = 1; j < getArg; j++){//traverse list until the
            temp = temp->next;          //sought-after node is found
        }
        temp->info = newData;//return the value of the sought-after node
    }
}
//LinkedList.h
#包括
使用名称空间std;
/*
类别:节点
描述:在“info”中保存一个T,在“info”中保存一个节点指针
“下一个”。链表的组成部分。
*/
模板
类节点{
公众:
T info;//保存info值
Node*next;//保存指向列表中下一个节点的指针
Node(T val);//构造函数
};
/*  
函数:构造函数
参数:T(值)
之前:
post:实例化节点,将“下一步”指针设置为NULL
例外情况:
返回:
*/
模板
Node::Node(T val){//构造函数,接受一个值来保存信息
info=val;
next=NULL;
}//结束节点类
///////////////////////////////////////////////
///////////////////////////////////////////////
/*
类别:LinkedList
描述:链接的T型节点列表。提供要添加的方法
节点并检索列表中给定位置的节点。
*/
模板
类链接列表{
公众:
Node*list;//指向列表的第一个节点
int size;//列表中的节点数
LinkedList();//默认构造函数
~LinkedList();//析构函数
void add(T addArg);//添加节点
T getAt(int getArg);//在“getArg”位置获取节点
void updateeat(int getArg,T newData);
};//结束LinkedList类
/*  
函数:链表默认构造函数
参数:
之前:
post:列表已实例化,大小设置为0
例外情况:
返回:
*/
模板
LinkedList::LinkedList(){
列表=空;
尺寸=0;
}
/*  
函数:链表析构函数
参数:
之前:
post:删除所有节点和指向第一个节点的指针
例外情况:
返回:
*/
模板
LinkedList::~LinkedList(){
while(list!=NULL){
节点*temp=list->next;
删除名单;
列表=临时;
}
}
/*  
功能:添加
参数:T(添加参数)
pre:列表被实例化
post:新节点已添加到该节点,上一个节点的链接
已设置为新节点。如果之前列表中没有节点
正在添加,新节点的链接为空
例外情况:
返回:无效
*/
模板
void LinkedList::add(T addArg){
如果(size==0){//如果列表为空
Node*next=new Node(addArg);//创建一个新节点
list=next;//并设置指向它的“list”指针
size++;//增加列表的大小
}
else if(size>0){//如果列表中至少有一个节点
Node*temp=list;//创建新节点
while(temp->next!=NULL){//遍历列表到最后一个节点
温度=温度->下一步;
}
temp->next=new节点(addArg);//设置最后一个节点的链接
//节点到值的新节点
//阿达格
size++;//增加列表的大小
}
else//抛出异常,列表的大小值为负值
抛出字符串(“由于某种原因,大小为负数…\n”);
}
/*  
功能:getAt
param(s):getArg(int,要检索的节点的位置)
pre:列表不是空的
职位:
异常:getArg的抛出边界异常为负值
还是出界
返回:位于“getArg”位置的节点的值
*/
模板
T LinkedList::getAt(int-getArg){
如果((getArg>=size)| |(getArgnext;//找到了寻找的节点
}
return temp->info;//返回所需节点的值
}
}
/*  
功能:updateeat
param(s):getArg(int,要检索的节点的位置)
新数据(T,新数据)
pre:列表不是空的
post:节点getArg上的信息更改为
异常:getArg的抛出边界异常为负值
还是出界
返回:位于“getArg”位置的节点的值
*/
模板
void LinkedList::updateAt(int-getArg,T-newData){
如果((getArg>=size)| |(getArgnext;//找到了寻找的节点
}
temp->info=newData;//返回所需节点的值
}
}
这是主要的

//main.cpp

void main(void)
{
    LinkedList<int> mine;
    mine.add(1);
    mine.add(2);
    mine.add(3);
    mine.add(4);
    mine.add(5);
    cout << "Size: " << mine.size << endl;

    int dem;
    for(int i = 1; i<= 5; i++)
    {
        dem = mine.getAt(i);
        cout << "i = " << i << endl << "val = " << dem << endl;
    }

    mine.updateAt(3, 10);
    for(int i = 1; i<= 5; i++)
    {
        dem = mine.getAt(i);
        cout << "i = " << i << endl << "val = " << dem << endl;
    }

}
//main.cpp
真空总管(真空)
{
LinkedList矿山;
增加第(1)款;
加上(2);
加上(3);
加上(4);
加上(5);

cout您正在访问一个越界索引:

for(int i = 1; i <= 5; i++)
它是基于0而不是基于1的

更合适的做法是:

for(int i = 0; i < 5; ++i)
for(int i=0;i<5;++i)

C/C++使用零索引。因此for循环应该是

for(int i = 0; i< 5; ++i)
for(int i=0;i<5;++i)
for(int i = 0; i< 5; ++i)