我的程序中有一个非常奇怪的错误。我在该程序中使用链表。 ead-> next确实显示为nullptr,但是跳过了if语句。奇怪的是,此后它不会转到else语句。我从逐步调试器中获得了此信息。我标记了包含该错误的函数部分。为什么会这样呢?我以为编译器出了点问题,但是我的程序在我的类服务器上经过了测试,并遇到了同样的问题。
void linked::deletetype(string what, node* ead) {
if (ead == nullptr) {
return;
}
else if (ead->type == what && ead->previous == nullptr) {
// it enters here
if (ead->next == nullptr) {// this is suppose to be true but it skips
ead == nullptr;
head == nullptr;
}
else {//even though it skips the if statment doesn't go to the else statement.
ead = ead->next;
ead->previous = nullptr;
head = ead;
}
// programs completely skips over else
}
else if (ead->type == what&&ead->next!=nullptr) {
ead->previous->next = ead->next;
ead->next->previous = ead->previous;
}
else if (ead->type == what && ead->next == nullptr) {
ead = ead->previous;
ead->next = nullptr;
tail = ead;
}
deletetype(what, ead->next);
}
最佳答案
我只是意识到我做了==而不是=在:
if (ead->next == nullptr) {// this is suppose to be true but it skips
ead == nullptr;
head == nullptr;
}
应该:if (ead->next == nullptr) {// this is suppose to be true but it skips
ead = nullptr;
head = nullptr;
}
谢谢大家的帮助!我不知道我怎么没注意到它。
关于c++ - if-else语句无法正常工作的怪异错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64330932/