Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 打印链接列表-访问冲突C++;_C++_Linked List - Fatal编程技术网

C++ 打印链接列表-访问冲突C++;

C++ 打印链接列表-访问冲突C++;,c++,linked-list,C++,Linked List,我有一个链表,它接收几个输入文件,然后将它们放入链表中,以便以后打印 我实现了一个打印函数,但它不能很好地工作,并给出了访问冲突错误。我试着调试,不幸的是我找不到问题的根源 函数中的错误行: cout << ptr2->command + " "; cout命令+“”; 运行时错误: 文件.exe中0x00DAC616处的首次偶然异常:0xC0000005:访问冲突读取位置0xCDCDCDDE1 代码如下: #include <iostream> #includ

我有一个链表,它接收几个输入文件,然后将它们放入链表中,以便以后打印

我实现了一个打印函数,但它不能很好地工作,并给出了访问冲突错误。我试着调试,不幸的是我找不到问题的根源

函数中的错误行:

cout << ptr2->command + " ";
cout命令+“”;
运行时错误:

文件.exe中0x00DAC616处的首次偶然异常:0xC0000005:访问冲突读取位置0xCDCDCDDE1

代码如下:

#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;
}
#包括
#包括
#包括
#包括“strutils.h”
使用名称空间std;
结构命令;
结构函数
{
字符串fname;
功能*对;
命令*向下;
};
结构命令
{
字符串命令;
命令*下一步;
};
职能*负责人;
功能*温度;
命令*temp2;
void StreamToLinkedList(ifstream&inputfile)
{
字符串s;
getline(输入文件,s);
temp=新函数();
temp->fname=s.substr(0,s.length());
temp2=temp->down;
而(!inputfile.eof())
{
getline(输入文件,s);
temp2=新命令();
temp2->command=s.substr(0,s.length()-1)+“,”;
temp2=temp2->next;
}
inputfile.clear();
inputfile.seekg(0);
}
void printLinkedList()
{
功能*ptr=头部;
命令*ptr2;
while(ptr!=nullptr)
{
无法命名下来;
while(ptr2!=nullptr)
{
cout命令+“”;
ptr2=ptr2->next;
}
正确;
}   
}
int main()
{
字符串文件,键,s;
ifstream输入;

cout访问冲突的问题是new在默认情况下不会将分配的内存归零, 所以最后一个结构中的指针指向一个随机值

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)

回答

您的打印功能正常。您创建和管理列表的方式是错误的,这可能是因为您从未初始化
函数。右
函数。向下
字段,因此链接列表链接到无效内存。实际上,您没有链接列表

您的一些赋值,例如
temp2=temp->down
temp=temp->right
没有任何意义,因为这些字段没有初始化,而且您正在用新对象覆盖这些变量(
temp
temp2

此外,您还有重复的代码。您正以完全相同的方式在两个不同的位置读取文件。问题是您的代码错误,因此需要修复两倍的错误。这里代码重复的唯一明显原因是您希望在用户第一次输入和第n次输入时显示不同的消息

我建议将这段代码放在它自己的函数中。否则,找到一种更有效的方法来使用条件/循环,这样就不会有太多重复的代码


注释

关于你的代码,我有“几点”需要注意。LinkedList分为两类,分别是
函数
命令
。我知道你想要一个函数列表,每个函数都有一个命令列表,但你必须学会将程序域与其他功能分开

IE:A
LinkedList
是对象(任何类型)的容器.A
函数
命令
是您为程序制作的与链表本身无关的特定东西。从概念上看,
函数
包含
命令
,但不一定通过
链接列表
,或
映射
,或
数组
,等等。

您的代码需要概念分离,看看它有多清晰

// Linked list of functions
Functions list;

// No need to comment this one
LinkedList functions;
(我们不要争论缺少模板类型)

此外,除非使用复数来命名一个类是有意义的,否则几乎总是使用单数(<代码>函数/代码> VS <代码>函数>代码>)。这是因为,当你实例化一个类时,你将有一个强< > <强>对象/实例,所以使用复数作为类型可能会误导。

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???
或默认构造函数

struct Functions {
    ...
    Functions() : right( nullptr ), down( nullptr ){}
};
然后初始化,比如

temp = new Functions();

你忘了问一个问题。你试着调试什么?你真的必须实现你自己的链表吗?vs std::list对于第一个问题,我调试它是因为我必须确保它真的工作;对于第二个问题,我必须实现我自己的链表。0xCDCDCD…对于Microsoft VC调试版本填充未初始化的数据是典型的。ThIS是扩展,在普通的“非调试”C++中,这样的区域将具有随机值。我应该初始化这些指针到NulLPTR?@ OGUZ,这样你就可以搜索模板的用法。编辑我的问题。
temp = new Functions;
temp->right = nullptr;
temp->down = nullptr;
struct Functions {
    ...
    Functions() : right( nullptr ), down( nullptr ){}
};
temp = new Functions();