gpt4 book ai didi

c++ - 抛出 'std::logic_error' What() 的实例后调用终止: basic_string::_S_construct null 无效

转载 作者:行者123 更新时间:2023-12-02 09:51:15 25 4
gpt4 key购买 nike

我一直在尝试将 cfg 读入多重链接列表,但我收到此错误,请有人帮忙。它不断给出这个错误。我不认为有什么错误。请检查这段代码并告诉我哪里有错误导致程序崩溃并出现图片中所附的异常


 #include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct prod_list{

string production;
struct prod_list *next;

}*head_p=NULL;

//Node for linked list to store Non terminals

struct rule_list{

char non_terminal;
struct prod_list *first;
struct rule_list *next;

}*head_r=NULL;

// Global variable
struct prod_list *temp_p,*prev_p=NULL,*first_p=NULL,*temp1_p=NULL,*temp2_p,*newTemp;
struct rule_list *temp_r,*prev_r=NULL,*first_r=NULL ,*temp1_r=NULL;



prod_list *adding_productions_of_the_rule(string line);
prod_list *add_node(prod_list *root, string production);


int main()
{

string line;

ifstream file ("cfg.txt");
if (file.is_open())
{
while ( getline (file,line) )
{
head_p = NULL;

// Creating rule_list node
temp_r = new rule_list;
temp_r->non_terminal = line[0];
head_p = adding_productions_of_the_rule(line);
temp_r->first = head_p;
temp_r->next = NULL;

if(head_r == NULL)
{
head_r = temp_r;
prev_r = temp_r;
}
else
{
prev_r->next = temp_r;
prev_r = temp_r;
}


}
file.close();
}
else
{
cout << "Unable to open file";
}

return 0;
}







prod_list *adding_productions_of_the_rule(string line)
{

string prod = NULL;

// Splitting line after arrow in cfg rule line.
line = line.substr(3,line.length());
// cout << "The cfg is "+line << endl;

// declaring character array of line's length
char ch[line.length()]={0};
// memset(ch,'0',line.length());
// cout << sizeof(ch) << endl;


// storing line in character array and printing character array
for(int i=0; i<line.length(); i++){
ch[i] = line[i];
// Displaying the characters stored in character array from string line.
// cout << ch[i];
}
// cout << endl;

char *point;
point = strtok(ch, "|");
while(point != NULL){

// cout << point << endl;

// Assigning each production from the cfg rule to an array of strings
// production[i] = point;
// cout << production[i] << endl;


// Creating prod_list node
prod = point;
head_p = add_node( head_p, prod );


point = strtok(NULL, "|");
// i++;

}


return head_p;
}






prod_list *add_node(prod_list *root, string production){


prod_list *next_ptr = NULL;
prod_list *prev_ptr = NULL;

if(root == NULL){
//list is empty
if( (root = new prod_list) != NULL){
next_ptr = root;
// cin >> next_ptr->title;
next_ptr->production = production;
next_ptr->next = NULL;

}else{
cout << "Error: Unable to allocate memory. " << endl;
return NULL;
}
return next_ptr;
}else{
//list has members.
next_ptr = root;
while(next_ptr->next != NULL){
next_ptr = next_ptr->next;
}
prev_ptr = next_ptr;

if((next_ptr = new prod_list)!= NULL){
prev_ptr->next = next_ptr;
next_ptr->production = production;
next_ptr->next = NULL;
}else{
cout << "Error: Unable to allocate memory " << endl;
}

}
return root;
}

**I have been trying to read cfg into a multilinklist but im getting this error please someone help **

/image/AgNXB.png

最佳答案

您的错误可以减少到最小的情况:

#include <string>
int main() {
std::string prod = NULL;
}

发生的事情是试图从 NULL 指针中创建一个 std::string 。显然,字符串没有任何东西可以使用,所以它做了它唯一能做的事情:尖叫求救。

解决方案:

不要分配任何东西。 std::string 将默认构造一个空字符串。

关于c++ - 抛出 'std::logic_error' What() 的实例后调用终止: basic_string::_S_construct null 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com