第一个问题解决后,我又遇到了另一个问题。它发生在 while 循环中
while (!infile.eof())
结果是一样的
while (getline(infile, line))
似乎不知何故它无法检查条件或其他东西,然后它再次崩溃。
[以下部分已解决]
这是函数。在插入大量 cout 以查看发生了什么之后,这
curr->prev = a->start;
是导致运行时错误的行。整个函数在这里:
busRoute readlist(const char* filename)
{
busRoute *a;
a = new busRoute; //a new bus route
stop_pointer curr;
stop_pointer pre;
fstream infile;
infile.open(filename);
string route="";
string line="";
int i=0;
float df, ef;
if (infile.is_open())
{
getline(infile, route); //read route number in string
a->routeNo=atoi(route.c_str()); //convert and store route num in int
while (!infile.eof())
{
i++;
getline(infile, line);//read text
//generate linked list
unsigned int pos;
string b,c,d,e;
pos = line.find(",");
if (pos <100000)
{
b = line.substr(0,pos); //stop name
c = line.substr(pos+1, line.length()-pos+2);
pos = c.find(",");
d = c.substr(0, pos); //latitude in string
e = c.substr(pos+1, c.length()-pos+2); //longitude in string
df = atof (d.c_str()); //latitude in float
ef = atof (e.c_str()); //longitude in float
//store b c d e into a
if (i<2)
{
a->start->stop_name= b;
a->start->latitude=df;
a->start->longitude=ef;
a->start->prev=NULL;
curr = a->start->next;
//sth wrong with curr->prev
curr->prev = a->start;
}
else
{
curr->stop_name=b;
curr->latitude=df;
curr->longitude=ef;
curr->next = new stop_node;
pre = curr;
curr = curr->next;
curr->prev = pre;
}
}
}
infile.close();
}
return *a;
}
下面是.h
typedef struct stop_node* stop_pointer;
typedef struct stop_node {
string stop_name; // the name of the stop
float latitude; // the latitude of the geographic coordinates of the stop
float longitude; // the longitude of the geographic coordinates of the stop
stop_pointer next; // the next pointer
stop_pointer prev; // the prev pointer
};
typedef struct busRoute {
int routeNo; // the route number of the stop
stop_pointer start; // the head of the linked list
};
有人可以向我解释我犯了什么错误吗?非常感谢。
最佳答案
我第一次认为 a->start->next;
指向一些垃圾值......
所以当你尝试
curr = a->start->next;
所以在这里你给 curr 分配了垃圾值
curr->prev = a->start;
在这里您正在尝试访问导致运行时错误的无效位置。希望这会有所帮助...
///为代码工作所做的更改...
if (i<2)
{
a->start->stop_name= b;
a->start->latitude=df;
a->start->longitude=ef;
a->start->prev=NULL;
//adding
a->start->next=NULL;
curr = a->start;
}
else
{
curr->next=new stop_node;
curr->next->prev=curr;
curr->stop_name=b;
curr->latitude=df;
curr=curr->next;
curr->next= NULL;
}
你的结构声明有问题...
删除这个
//typedef struct stop_node* stop_pointer;
typedef struct stop_node {
string stop_name; // the name of the stop
float latitude; // the latitude of the geographic coordinates of the stop
float longitude; // the longitude of the geographic coordinates of the stop
stop_node *next; // the next pointer
stop_node *prev; // the prev pointer
};
typedef struct busRoute {
int routeNo; // the route number of the stop
//stop_pointer start; // the head of the linked list
stop_node start;
};
如果 start
是指针类型;您需要动态分配内存然后访问内存。所以它会像这样
a->start = new bus_node();
为避免复杂化,请使用上面的 struct 和 Use 定义。运算符在其余代码中访问 start 的成员。例如。 a->start.next = NULL
关于c++ - 运行时错误。我知道在哪里,但我不知道为什么以及如何纠正它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22097358/