- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个链表,它接受几个输入文件,然后将它们放入一个链表中,以便稍后打印。
我实现了打印功能,但效果不佳,并出现访问冲突错误。我尝试调试,不幸的是我找不到问题的根源。
函数中的错误行:
cout << ptr2->command + " ";
运行时错误:
First-chance exception at 0x00DAC616 in the file.exe: 0xC0000005: Access violation reading location 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;
}
错误代码可能是什么?
最佳答案
回答
你的打印功能没问题。您创建和管理列表的方式出了问题,可能是因为您从未初始化您的Functions.right
和Functions.down
字段,因此您的链表链接到无效内存。好吧,您实际上并没有链接您的列表。
你的一些赋值,比如 temp2 = temp->down
和 temp = temp->right
没有任何意义,因为这些字段没有初始化,因为你之后用新对象覆盖这些变量(temp
和 temp2
)。
此外,您有重复的代码。您正在以完全相同的方式在两个不同的地方读取文件。问题是你的代码是错误的,所以你有双倍的错误要修复。此处代码重复的唯一明显原因是您希望在用户第一次输入和第 n 次输入时显示不同的消息。
我建议将这段代码放在它自己的函数中。否则找到一种更有效的方法来使用条件/循环,这样您就不会有那么多重复代码。
注意事项
我有一些关于您的代码的注意事项。 LinkedList 在名为 Functions
和 Commands
的两个类中实现。我知道您想拥有一个函数列表并且每个函数都有一个命令列表,但是您必须学会将程序的域与其他功能区分开来。 p>
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
列表,所以您有一个列表列表,您不必像在当前实现中那样独立管理每个列表.当然,您的列表仍然需要一个接口(interface)(和内存管理),但这不在这个问题的范围内。
编辑:
当它们不指向任何东西时,将你的指针初始化为 nullptr。
temp = new Functions;
temp->right = nullptr;
temp->down = nullptr;
或者使用默认构造函数,
struct Functions {
...
Functions() : right( nullptr ), down( nullptr ){}
};
然后像这样初始化
temp = new Functions();
关于c++ - 打印链接列表 - 访问冲突 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33316863/
我有一个网站,并且我使用 javascript sdk 添加了“点赞”按钮。 这是代码 (function(d, s, id) { var js, fjs = d.g
我知道 HTML 是逐行读取的。当您链接多个 css 文件(如规范化文件和样式表文件)时,由于 CSS 重要性特异性和源顺序,样式表文件应链接在规范化文件之后。看起来这不会影响链接的 JavaScri
我正在使用官方 Bootstrap site 提供的 CDN 链接在我的网络应用程序中使用面板进行测试 在彻底检查我的代码后,面板没有显示。 但是我在 SO 上看到了类似的帖子并且 CDN 链接不同
这里是编码初学者。我正在尝试为我的移动设备网站设置断点,以便我的网站适合小屏幕。我只是想检查如果我缩小视口(viewport)的宽度,背景颜色是否会改变,但没有发生任何变化。也许我只是对一个简单的错误
举一个我想要的例子,想象一下这个字符串: $text = 'lorem ipsum About us lorem ipsum'; 如果此字符串包含一个 href 以 / 开头的 anchor 链接,则
如何链接到 LaTeX 文档的另一部分或子部分?这种链接的常规范式是什么,像[链接名称]那样写,或者像网页超链接那样写? 最佳答案 链接到另一个部分需要您的部分进行一些额外的标记。要使用的命令是: \
我有一个订单表,其中包含订单号、客户 ID 和代理 ID。然后有一个带有 id 的客户表和一个带有 id 的代理表。 我需要获取所有具有来自代理 ID 'a03' 和代理 ID 'a05' 的订单的客
假设我有: dic = {"z":"zv", "a":"av"} ## Why doesn't the following return a sorted list of keys? keys = d
我在尝试链接到外部库时得到了一些奇怪的结果。如果我从命令行运行以下命令: gcc fftwTest.c -I../extlib/fftw-3.3.5-dll32 -L../extlib/fftw-3.
我认为我没有正确理解 jQuery 链接。我正在遍历一个数组并尝试将 div 元素添加到我的包装器 CSS 类中,每个 div 元素都有一个“click”类和自定义 css top 和 left 属性
HTML 使用超级链接与网络上的另一个文档相连。几乎可以在所有的网页中找到链接。点击链接可以从一张页面跳转到另一张页面。 HTML 超链接(链接) HTML使用标签 a 来设置超文本链接。 超链
这个问题在这里已经有了答案: How do I link to part of a page? (hash?) (7 个答案) Scroll Automatically to the Bottom
我想创建一个 Docker Swarm 集群,运行一个 Elasticsearch 实例、一个 MongoDB 实例和一个 grails 应用程序,每个都在单独的机器上。我正在使用 Docker Ma
我正在尝试将 CakePHP HTML Linker 用于以下代码 Add Cuisine 由于 span 标签需要在 a 标签内。我无法根据需要获得输出。关于如何完成它的任何建议? 最佳答案 禁用链
大家好, 我最近开发了一个应用程序,很快就会提交到 App Store。我想免费提交这个应用程序,并想知道我是否可以实现一个带有 PayPal 捐赠标志的按钮,上面基本上写着“捐赠用于开发”或与此相关
我想尝试在 dlang 中使用 libuv。我下载了这样的 dlang 绑定(bind): git clone git@github.com:tamediadigital/libuv.git 现在我接
我有一个节点(节点 a),各种其他节点(节点 b/c/d/e)与之引用。 我可以创建一个带有参数的 View 作为我正在查看的节点(节点 a),并获取引用该节点的节点列表。 基本上在节点 a 查看节点
我正在尝试建立一个常见问题页面,上面有目录,下面有答案。我想点击目录中的一个问题,并在同一页面上链接到相应的答案。我如何在 CakePHP 中使用 $this->Html->link() 执行此操作方
在 WooCommerce 3.0+ 中,我使用 js 创建了一些选项卡,每个选项卡中包含来自不同类别的产品。我已经设法修改了简单产品的添加到购物车链接,其中点击了 addtocart 按钮它进入下一
Delphi 2007/2009 奇怪的问题在这里: 根据设计时定义的组件属性,是否可以在链接中包含文件或保留文件? 示例:如果我将 SomeProperty 保留为真,则在编译时,单元 SomeUn
我是一名优秀的程序员,十分优秀!