C++ 运行时错误。我知道在哪里,但我不知道;我不知道为什么以及如何纠正它
第一个问题解决后,我又遇到了另一个问题。它发生在while循环上C++ 运行时错误。我知道在哪里,但我不知道;我不知道为什么以及如何纠正它,c++,eclipse,pointers,struct,linked-list,C++,Eclipse,Pointers,Struct,Linked List,第一个问题解决后,我又遇到了另一个问题。它发生在while循环上 while (!infile.eof()) 如果是,结果是一样的 while (getline(infile, line)) 似乎不知怎么的,它没有检查到状况或其他什么,然后它又崩溃了 [以下部分已解决] 这就是函数。在插入数吨cout以查看发生了什么之后 curr->prev = a->start; 是导致运行时错误的行。整个功能如下: busRoute readlist(const char* filenam
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;
}
有人能解释一下我犯了什么错误吗?非常感谢。我第一次想到
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;
}
如果start
是指针类型;您需要动态分配内存,然后访问内存。所以事情会是这样的
a->start=新总线节点()
为了避免复杂性,请使用上面的struct和use定义。运算符访问代码其余部分中的start成员。例如,a->start.next=NULL
您能解释一下运行时错误的确切含义吗?程序是否崩溃,或者是否以意外的方式运行?使用eof()
几乎总是一个bug。改为说while(getline(infle,line))
。@Frank它崩溃了。正常的“ABC.EXE已经停止工作。Windows可以检查在线BLAH BLAH”,“MalbDNILO谢谢”,但这并不能解决当前的问题。@路易斯:好吧,这个代码中没有太多C++,它感觉像更多的C。这是某种练习还是真正的代码?在实际代码中,如果您想要一个链表,而不是试图自己实现它,那么您可以使用标准的std::list类。如何将->开始->下一步更改为非垃圾值?现在它卡在了其他地方。它运行if(我已经添加了查找…请看它现在应该可以工作了…如果您使用linux或visual studio在windows中编写程序,请使用gdb,而不是使用cout进行调试。。。
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;
};