代码不会因为"not all paths return value"而编译,即使从逻辑上讲它们是编译的



这是我在BTree类中的搜索方法:

public bool Search(string val)
    {
        if (String.Compare(Value, val, false) == 0)
            return true;
        if (String.Compare(Value, val, false) < 0)
        {
            if (RightChild != null)
                RightChild.Search(val);
            else return false;
        }
        else
        {
            if (LeftChild != null)
                LeftChild.Search(val);
            else return false;
        }
    }

这段代码无法编译,因为并非所有路径都返回值(我所有的返回都是有条件的),即使从逻辑上讲它们都这样做。我不知道如何解决这个基本问题。也许我的整个方法都是错误的?

public bool Search(string val)
    {
        if (String.Compare(Value, val, false) == 0)
            return true;
        if (String.Compare(Value, val, false) < 0)
        {
            if (RightChild != null)
                return RightChild.Search(val); // added 'return'
            /*[REMOVED]else return false;*/
        }
        else
        {
            if (LeftChild != null)
                return LeftChild.Search(val);  // added 'return'
            /*[REMOVED]else return false;*/
        }
        return false;
    }

将返回语句添加到 RightChild.Search 和 LeftChild.Search 调用中。 我假设这是某种 Node 类的成员,这些是递归调用。

相关内容

最新更新