警告C4715:并非所有控制路径都返回值c ++ [英] warning C4715: not all control paths return a value c++

查看:68
本文介绍了警告C4715:并非所有控制路径都返回值c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了两个函数,一个在红黑树中查找并返回最小键,另一个函数以该节点的键作为输入返回指向特定节点的指针.最高键和最低键.程序停止运行,并发出C4716警告.关键是int array [] = {50,26,45,34,23,78,84,93,14,16,100,57,62};

  int Tree :: findsmallest(){返回findsmallestprivate(root);}int Tree :: findsmallestprivate(node * ptr){如果(root!= NULL){如果(ptr-> left!= NULL){findsmallestprivate(ptr-> left);}别的{返回ptr-> key;}}别的{cout<<没有树"<<恩德尔返回-1;}}Tree :: node * Tree :: returnnode(int键){返回returnnodepri(key,root);}Tree :: node * Tree :: returnnodepri(int键,node * ptr){如果(ptr-> key == key){返回ptr;}否则如果(ptr-> key< key){returnnodepri(key,ptr-> right);}否则(ptr-> key> key){returnnodepri(key,ptr-> left);}别的{返回NULL;}} 

解决方案

if(ptr-> left!= NULL)中,您无法返回值,如编译器所说.您需要返回一个值.

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 . the keys are 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. You need to return a value.

这篇关于警告C4715:并非所有控制路径都返回值c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆