【问题标题】:Strange error in C++ without stoping the programC ++中的奇怪错误而不停止程序
【发布时间】:2022-01-12 18:05:31
【问题描述】:

当我更改传递给数组的值时,我收到了一个奇怪的错误,它不会停止程序,但它总是会出现,所以我很好奇为什么。我在下面给你我的代码: 我的错误类似于: In function 'int numOfDifferentElements(BSTree*, int)': control reach end of non-void function [-Wreturn-type] (main.cpp:97:1) 并且有一个箭头指向: }

    #include <iostream>

using namespace std;

struct BSTree
{
    int val;
    BSTree* up;
    BSTree* right;
    BSTree* left;
};
void AddBstNode(BSTree* &root, int valToAdd)
{
    {
        BSTree* pom;
        BSTree* nodeToAdd = new BSTree;
        nodeToAdd->val = valToAdd;
        nodeToAdd->left = NULL;
        nodeToAdd->right = NULL;
        nodeToAdd->up = NULL;

        if (root == NULL)
        {
            root = nodeToAdd;
        }
        else
        {
            pom = root;
            while (1)
            {
                if (nodeToAdd->val < pom->val)
                    if (pom->left == NULL)
                    {
                        pom->left = nodeToAdd;
                        break;
                    }
                    else
                        pom = pom->left;
                else if(nodeToAdd->val > pom->val)
                {
                    if (pom->right == NULL)
                    {
                        pom->right = nodeToAdd;
                        break;
                    }
                    else
                        pom = pom->right;
                }
                else // gdy wartosc  jest rowna to jej nie dodajemy do drzewka aby potem mozna bylo zliczyc el drzewa
                // a kazdy z nich bedzie inny
                {
                    break;
                }
            }
            nodeToAdd->up = pom;
        }
    }
}
int numOfDifferentElements(BSTree* root, int counter)
{
    if (root)
    {
        counter++;
        numOfDifferentElements(root->left, counter);
        numOfDifferentElements(root->right, counter);
    }
    else
    {
        return counter;
    }
}
void judgeArray(int array[], int x)
{
    BSTree* pom = NULL;
    int lengthOfArray = *(&array +1 ) - array; // rozmiar naszej tablicy
    for(int i = 0; i < lengthOfArray; i++)
    {
        AddBstNode(pom, array[i]); // tworzymy drzewo z elementow naszej tablicy
    }
    int counter = 0;
    int judge = numOfDifferentElements(pom, counter); // zliczamy liczbe el drzewa, bez zadnych zalozen, bo
    //wszystkie jego elementy sa rozne co zostalo zapewnione w funkcji AddBstNode ( kom. w linii 76 i 77)
    if(judge == x)
    {
        cout<<"Array is good"<<endl;
    }
    else if(judge < x)
    {
        cout<<"Array is wrong"<<endl;
    }
    else if(judge > x)
    {
        cout<<"Array is mediocre"<<endl;
    } // powyzej nasze zalozenia, jesli liczba roznych el jest mniejsza/wieksza/rowna od naszego zalozonego x ( liczby
    //roznych elementow) to tak oceniamy nasza tablice
}
int main() {
    int N[16] = {1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8}; // przykladowa tablica
    int x = 12; // przykladowa wart x
    judgeArray(N, x); // wywolanie naszej funkcji z przykladowa wartoscia
    return 0;
}

【问题讨论】:

  • 如果该函数通过if 分支,则没有return 语句。仅在 else 分支中有一个。在if 中也添加一个,或者在函数末尾添加一个。
  • 注意:C++ 中的[-Wreturn-type] 相当严重。当它发生时,gcc 和 clang 都会生成非常糟糕的代码。我建议使用-Werror=return-type 编译所有内容。
  • 非常感谢您的帮助,它帮助了我和我的代码都可以正常工作,而且这是我在这里的第一次发布,所以我对如此快速的答案感到惊讶!

标签: c++ algorithm


【解决方案1】:

问题是在您的numOfDifferentElements 函数中,如果满足if 条件,则没有返回语句。问题是因为函数的返回类型是非空的,所以你必须返回一些东西。您可以通过在if 块内或else 块外(之后)添加return 语句来解决此问题,如下所示:

int numOfDifferentElements(BSTree* root, int counter)
{
    if (root)
    {
        counter++;
        numOfDifferentElements(root->left, counter);
        numOfDifferentElements(root->right, counter);
        //can also add a return statement here
    }
    else
    {
        return counter;
    }
    //or add return -1; or some other value indicating ERROR CODE  
}

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2016-07-07
    • 1970-01-01
    • 2015-02-05
    • 2023-04-05
    • 1970-01-01
    • 2015-11-11
    • 2012-01-29
    • 1970-01-01
    相关资源
    最近更新 更多