c++ - if-else语句无法正常工作的怪异错误

标签 c++ if-statement

我的程序中有一个非常奇怪的错误。我在该程序中使用链表。 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/

相关文章:

c++ - std::condition_variable::notify_one() 调用两次

mysql - 选择一个字段。如果为空,则选择另一个

c++ - 在 C++ 中如何为结构类型分配内存

java - java中if语句中的while循环

c++ - 为什么检查赋值运算符的结果有效,但不能与其他条件结合使用?

if-statement - 当for循环内遇到if语句时跳过for循环

java - elsif 与 nextboolean 用于图片

c++ - Visual C++ 错误

c++ - 如何使用 native 代码中的 JNI 在 Android 上获取音频音量?

javascript - 如何将 const void * 传递给 node.js?