[英]Printing A linked List - Access Violation C++
我有一个链接列表,它接受几个输入文件,然后将它们放入链表中以便稍后打印。
我实现了一个打印功能,但它不能正常工作并且提供访问冲突错误。 我试图调试,不幸的是我找不到问题的根源。
函数中的错误行:
cout << ptr2->command + " ";
运行时错误:
file.exe中0x00DAC616的第一次机会异常:0xC0000005:访问冲突读取位置0xCDCDCDE1。
这是代码:
#include <iostream>
#include <fstream>
#include <string>
#include "strutils.h"
using namespace std;
struct Commands;
struct Functions
{
string fname;
Functions *right;
Commands *down;
};
struct Commands
{
string command;
Commands *next;
};
Functions *head;
Functions *temp;
Commands *temp2;
void StreamToLinkedList(ifstream &inputfile)
{
string s;
getline(inputfile, s);
temp = new Functions();
temp->fname = s.substr(0, s.length());
temp2 = temp->down;
while (!inputfile.eof())
{
getline(inputfile, s);
temp2 = new Commands();
temp2->command = s.substr(0, s.length()-1) + ",";
temp2 = temp2->next;
}
inputfile.clear();
inputfile.seekg(0);
}
void printLinkedList()
{
Functions *ptr = head;
Commands *ptr2;
while (ptr != nullptr)
{
cout << ptr->fname << endl;
ptr2 = ptr->down;
while (ptr2 != nullptr)
{
cout << ptr2->command + " ";
ptr2 = ptr2->next;
}
cout << endl;
ptr = ptr->right;
}
}
int main()
{
string file, key, s;
ifstream input;
cout <<"If you want to open a service (function) defining the file," << endl
<<"then press (Y/y) for 'yes', otherwise press any single key" << endl;
cin >> key;
ToLower(key);
if (key == "y")
{
cout << "Enter file the input file name: ";
cin >> file;
input.open(file.c_str());
if (input.fail())
{
cout << "Cannot open the file." << endl
<< "Program terminated." << endl;
cin.get();
cin.ignore();
return 0;
}
else
{
StreamToLinkedList(input);
head = temp;
temp = temp->right;
}
}
else
{
cout << "Cannot found any input file to process" <<endl
<< "Program terminated."<< endl;
cin.get();
cin.ignore();
return 0;
}
do
{
cout<< "Do you want to open another service defining file?"<<endl
<< "Press (Y/y) for 'yes', otherwise press any key" <<endl;
cin >> key;
ToLower(key);
if (key == "y")
{
cout << "Enter file the input file name: ";
cin >> file;
input.open(file.c_str());
if (input.fail())
{
cout << "Cannot open the file." << endl
<< "Program terminated." << endl;
cin.get();
cin.ignore();
return 0;
}
else
{
StreamToLinkedList(input);
temp = temp->right;
}
}
} while ( key == "y");
cout << "-------------------------------------------------------------------" << endl
<< "PRINTING AVAILABLE SERVICES (FUNCTIONS) TO BE CHOSEN FROM THE USERS" << endl
<< "-------------------------------------------------------------------" << endl << endl;
printLinkedList();
cin.get();
cin.ignore();
return 0;
}
可能是错误的代码是什么?
你的访问冲突问题是新的默认情况下不会将其分配的内存归零,因此最后一个结构内的指针指向一个随机值。
temp2 = new Commands; // this memory is not initilized to zero
temp2 = new Commands(); // this memory is initialized to zero, (all elements is set to 0)
回答
你的打印功能还可以。 您创建和管理列表的方式是错误的,可能是因为您从未初始化 Functions.right
和Functions.down
字段,因此链接列表链接到无效内存。 好吧,你实际上没有链接你的列表。
你的一些赋值如temp2 = temp->down
和temp = temp->right
并不意味着什么,因为这些字段没有初始化,因为你之后用新对象覆盖了这些变量( temp
和temp2
)。
此外,您有重复的代码。 您正在两个不同的位置以完全相同的方式从文件中读取文件。 问题是您的代码错误,因此您需要修复两倍的错误。 这里代码重复的唯一明显原因是,您希望在用户第一次输入输入和第n次时显示不同的消息。
我建议将此代码放在自己的函数中。 否则找到一种更有效的方法来使用条件/循环,这样你就没有那么多重复的代码了。
笔记
我的代码中有一些“注意事项”。 LinkedList在两个名为Functions
和Commands
类中实现。 我知道你想要一个函数列表,每个函数都有一个命令列表 ,但是你必须学习将程序的域与其他功能分开。
IE: LinkedList
是对象(任何类型)的容器。 Function
和Command
是您为程序创建的与链接列表本身无关的特定事项。 从概念的角度来看, Function
包含Command
,但不一定是通过LinkedList
, map
或array
等。
您的代码需要分离概念,看看它有多清楚
// Linked list of functions
Functions list;
// No need to comment this one
LinkedList functions;
(我们不要说缺少模板类型)
另外,除非使用复数来命名一个类,否则你应该几乎总是使用单数( Functions
与Function
)。 那是因为当您实例化一个类时,您将拥有一个对象/实例,因此使用该类型的复数可能会产生误导。 考虑以下声明,
Function someFunction; // This is a single function
Functions someFunction; // Is this one function or multiple functions??
List< Function > functions; // This is a list of functions
List< Functions > functions; // Is this a list of lists of functions???
您应该创建一个名为LinkedList的类(提示:如果可以使模板更可重用,请使用模板)。 然后,如果你要从链表中分离函数和命令的实现,你可以做类似的事情,
template <class T>
struct LinkedList {
T* obj;
LinkedList<T>* next;
LinkedList() : obj( nullptr ), next( nullptr ) {}
};
struct Command {
string cname;
};
struct Function {
string fname;
LinkedList<Command> commands;
};
现在你要声明这样的函数列表,
LinkedList<Function> functions;
由于每个Function
都有自己的Command
列表,因此您有一个列表列表,而不必像在当前实现中那样独立管理每个列表。 当然,您的列表仍然需要一个接口(和内存管理),但这不在本问题的范围内。
编辑:
当它们不指向任何东西时,将指针初始化为nullptr。
temp = new Functions;
temp->right = nullptr;
temp->down = nullptr;
或美国的默认构造函数,
struct Functions {
...
Functions() : right( nullptr ), down( nullptr ){}
};
然后初始化,像,
temp = new Functions();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.