c++ - 尝试创建链表时出现错误“写访问冲突。this->tail was nullptr”
问题描述
我正在尝试将 csv 文件中的数据输入到链接列表中。
程序编译但没有输出。我正在使用调试器逐行浏览代码,一切似乎都很顺利,直到我被带到该行:tail->next = temp 然后出现一个错误,指出“抛出未处理的异常:写访问冲突。这- >tail 是 nullptr”。每次我尝试执行另一行代码时,我都会被带回到那个点。
struct Customer {
std::string raw_User_ID;
std::string Product_ID;
std::string raw_Gender;
std::string age;
std::string raw_Occupation;
std::string raw_City_Category;
std::string Stay_In_Current_City_Years;
std::string raw_Marital_Status;
std::string raw_Product_Category_1;
std::string raw_Product_Category_2;
std::string raw_Product_Category_3;
std::string raw_Purchase;
std::string allData;
double User_ID;
char Gender;
int Occupation;
char City_Category;
bool Marital_Status;
int Product_Category_1;
int Product_Category_2;
int Product_Category_3;
double Purchase;
Customer *next;
};
class List
{
private:
Customer *head, *tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void inputCustomers(std::ifstream& infile) {
infile.open("BlackFriday.csv");
if (!infile) {
std::cout << "The file could not be found\n";
}
else {
std::cout << "The file was found\n";
}
while (!infile.eof()) {
Customer *temp = new Customer;
std::getline(infile, temp->raw_User_ID, ',');
std::getline(infile, temp->Product_ID, ',');
std::getline(infile, temp->raw_Gender, ',');
std::getline(infile, temp->age, ',');
std::getline(infile, temp->raw_Occupation, ',');
std::getline(infile, temp->raw_City_Category, ',');
std::getline(infile, temp->Stay_In_Current_City_Years,
',');
std::getline(infile, temp->raw_Marital_Status, ',');
std::getline(infile, temp->raw_Product_Category_1,
',');
std::getline(infile, temp->raw_Product_Category_2,
',');
std::getline(infile, temp->raw_Product_Category_3,
',');
std::getline(infile, temp->raw_Purchase, '\n');
if (temp->raw_Product_Category_1 == "") {
temp->raw_Product_Category_1 = '0';
}
if (temp->raw_Product_Category_2 == "") {
temp->raw_Product_Category_2 = '0';
}
if (temp->raw_Product_Category_3 == "") {
temp->raw_Product_Category_3 = '0';
}
std::stringstream(temp->raw_User_ID) >> temp->User_ID;
std::stringstream(temp->raw_Gender) >> temp->Gender;
std::stringstream(temp->raw_Occupation) >> temp->Occupation;
std::stringstream(temp->raw_City_Category) >> temp->City_Category;
std::stringstream(temp->raw_Marital_Status) >> temp->Marital_Status;
std::stringstream(temp->raw_Product_Category_1) >> temp->Product_Category_1;
std::stringstream(temp->raw_Product_Category_2) >> temp->Product_Category_2;
std::stringstream(temp->raw_Product_Category_3) >> temp->Product_Category_3;
std::stringstream(temp->raw_Purchase) >> temp->Purchase;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
tail->next = temp;
tail = temp->next;
}
}
}
void displayhead() {
Customer *temp = new Customer;
temp = head;
std::cout << temp->age << std::endl;
std::cout << temp->Gender << std::endl;
std::cout << temp->allData << std::endl;
}
};
int main() {
List AllCustomers;
std::ifstream input;
AllCustomers.inputCustomers(input);
AllCustomers.displayhead();
getchar();
return 0;
}
解决方案
我怀疑这对于前两行输入运行良好。您设置temp->next = NULL;
, 并在 else 子句中执行,在第三次运行中tail = temp->next;
有效地设置tail
为。NULL
由于tail
是指向最后一个元素,只需更改
tail = temp->next;
至
tail = temp;
在 else 子句中。
推荐阅读
- swift - 如何为 UIButton ImageView Swift 设置约束?
- powershell - 如何在 Powershell 脚本中为文件名传递通配符
- java - 如何从 Freemarker 中的模板数据映射对象递归打印数据?
- sql - 如何在laravel的json数组列中找到字符串搜索的位置
- python - 在视频游戏中读取视觉法力条
- javascript - javascript函数声明后引导模式不起作用
- r - 更改栏内文本的大小
- javascript - 蒙版背景图片减法画布(如果可能的话纯js)
- jackson - 使用jackson时如何根据上下文Bean类型序列化属性?
- javascript - 如何在反应功能组件中使用动画 svg?