简体   繁体   English

警告 C4715:并非所有控制路径都返回值 C++

[英]warning C4715: not all control paths return a value c++

i made two functions, one to find and return smallest key in a red - black tree and the other one returns a pointer to a particular node with that node's key as input.These functions work fine with all nodes except the nodes with the highest key and the lowest key.The program stops working and gives the C4716 warning .我做了两个函数,一个用于查找并返回红黑树中最小的键,另一个返回一个指向特定节点的指针,该节点的键作为输入。这些函数适用于除具有最高键的节点之外的所有节点和最低键。程序停止工作并给出 C4716 警告。 the keys are int array[] = { 50, 26, 45, 34, 23, 78, 84, 93, 14, 16, 100, 57, 62};键是 int array[] = { 50, 26, 45, 34, 23, 78, 84, 93, 14, 16, 100, 57, 62};

int Tree::findsmallest()
{
return  findsmallestprivate(root);
}

int Tree::findsmallestprivate(node* ptr)
{
if (root != NULL)
{
    if (ptr->left != NULL)
    {
        findsmallestprivate(ptr->left);
    }
    else
    {
        return ptr->key;
    }
}
else
{
    cout << "There was no tree" << endl;
    return -1;
}
} 
Tree::node* Tree::returnnode(int key)
{
return returnnodepri(key, root);
}
Tree::node* Tree::returnnodepri(int key, node* ptr)
{
    if (ptr->key == key)
    {
        return ptr;
    }
    else if (ptr->key < key)
    {
        returnnodepri(key, ptr->right);
    }
    else if (ptr->key > key)
    {
        returnnodepri(key, ptr->left);
    }
else
{
    return NULL;
}
}

In if (ptr->left != NULL) you fail to return a value, as the compiler says.正如编译器所说,在if (ptr->left != NULL)您无法返回值。 You need to return a value.您需要返回一个值。

In findsmallestprivate :findsmallestprivate

If condition ptr->left != NULL holds, you do not return anything.如果条件ptr->left != NULL成立,则不会返回任何内容。 You just run findsmallestprivate(ptr->left);你只需运行findsmallestprivate(ptr->left); and then exit, but don't return expected int .然后退出,但不要返回预期的int

warning C4715: not all control paths return a value means that you have a function which may not return a value sometimes depending of its input. warning C4715: not all control paths return a value意味着您的函数有时可能不返回值,具体取决于其输入。

Your other problems are the same as with findsmallestprivate .您的其他问题与findsmallestprivate相同。

In returnnodepri :returnnodepri

In case of ptr->key < key or ptr->key > key you don't return expected Tree::node* .ptr->key < keyptr->key > key情况下,您不会返回预期的Tree::node* You run returnnodepri , but not return any value as a result.您运行returnnodepri ,但结果不返回任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM