System.AccessViolationException尝试读取或写入受保护的内存

时间:2012-02-23 22:43:50

标签: c++ data-structures linked-list

您好我正在使用一个链接列表数组但我无法运行它的程序。我一直收到这个错误,我找不到解决方法。我只会包含部分代码,因为一切都不会太杂乱。错误消息是说NodeADT.h中的第112行,MultiListADT.h中的第141行和main.cpp中的第21行是抛出错误的行。我会突出显示这些线条以使其更容易。

Main.cpp的

#include <iostream>
#include "MultiListADT.h"
#include <fstream>
#include <string>

using namespace std;

void main(void)
{
MultiListADT<string,100> myList;
string item;
ifstream data;
string input;
int x=0;

data.open("input.txt");

while (!data.eof())
{
    getline(data,input);
    myList.AddToFront(input);        //This is line 21
}

cout << myList << endl;

system("pause");
}

MultiListADT.h

#include <iostream>
#include <fstream>
#include "NodeADT.h"
#include <string>

using namespace std;

template <class TYPE,int threads>
class MultiListADT
{
public:
/** Constructor **/
MultiListADT();

/** Destructor **/
~MultiListADT();

/** Declare accessors (observers) **/
void ResetListForward(int=0);
void ResetListBackward(int=0);
bool IsEmpty(int=0);
int LengthIs(int=0);
bool Search(string, bool=true,int=0);
void GetNextItem(TYPE &,int i=0);
void GetPreviousItem(TYPE &,int=0);
int GetInfo(int=0);
friend ostream& operator << (ostream&, MultiListADT<TYPE, 100>&);

/** Declare mutators (transformers) **/
void MakeEmpty();
void AddToFront(TYPE);
void AddToRear(TYPE);
void InsertInOrder(TYPE);
void Delete(TYPE);
void Sort();

private:
NodeADT<TYPE,threads>* head[threads];
NodeADT<TYPE,threads>* tail[threads];
int length;
string indices[threads];
NodeADT<TYPE,threads>* currentNode[threads];
};

template <class TYPE,int threads>
MultiListADT<TYPE,threads>::MultiListADT()
{
head[threads] = new NodeADT<string,threads>(); 
tail[threads] = new NodeADT<string,threads>();
head[threads]->setNext(tail[threads]);
tail[threads]->setPrevious(head[threads]);
length = 0;
}

template <class TYPE,int threads>
void MultiListADT<TYPE,threads>::AddToFront(TYPE item)
{
head[0]->AddToFront(item);        //This is line 141

length++;
}

NoteADT.h

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int null = 0;

template<class TYPE, int threads>
class MultiListADT;

template <class TYPE, int threads>
class NodeADT
{
public:
NodeADT();
NodeADT(TYPE);
~NodeADT();
TYPE getInfo();
NodeADT<TYPE, threads>* getPrevious(int=0);
NodeADT<TYPE, threads>* getNext(int=0);
void setNext(NodeADT<TYPE, threads>*,int=0);
void setPrevious(NodeADT<TYPE, threads>*,int=0);
bool Search(TYPE, bool=true,int=0);
void AddToFront(TYPE item);
void AddToRear(TYPE item);
void InsertInOrder(TYPE);
bool Delete(TYPE);
friend ostream& operator << (ostream&, MultiListADT<TYPE, threads>&);
private:
TYPE info;
NodeADT<TYPE, threads>* prev[threads];
NodeADT<TYPE, threads>* next[threads];
};

template <class TYPE,int threads>
NodeADT<TYPE,threads>::NodeADT()
{
prev[threads] = null;
next[threads] = null;
}

template <class TYPE,int threads>
NodeADT<TYPE,threads>::NodeADT(TYPE item)
{
info = item;
prev = null;
next = null;
}

template <class TYPE,int threads>
void NodeADT<TYPE,threads>::AddToFront(TYPE item)
{
NodeADT<TYPE,threads> *temp = new NodeADT<TYPE,threads>;
temp->info = item;  
temp->prev[0] = this;
temp->next[0] = next[0];
next[0]->prev[0] = temp;        //This is line 112
next[0] = temp;
}

2 个答案:

答案 0 :(得分:0)

认为错误意味着什么?

在第112行,next,prev和temp的值来自何处以及它们在崩溃时设置为什么?了解这些价值观,为什么你认为它崩溃了?

同样在一个NodeADT构造函数中,您将null赋给数组的最后一个元素。或者它出现了。

问题:当元素计数从0开始时,为100个元素的数组中的编号为100的元素赋值时会发生什么?

答案 1 :(得分:0)

我认为Zan Lynx暗示的答案是,您在threads的构造函数中使用MultiListADT作为数组的索引。在AddToFront中,您使用0作为索引,但数组中的该元素从未初始化。