gpt4 book ai didi

c++ - 递归宇宙飞船运算符

转载 作者:行者123 更新时间:2023-12-02 16:13:35 25 4
gpt4 key购买 nike

我写了这个简单的代码,但它没有编译,因为比较被隐式删除。

struct Tree {
std::vector<Tree> child;

friend auto operator<=>(const Tree &a, const Tree &b) = default;
}
int main(){
Tree t;
std::cout<<(t<t)<<std::endl;
}

任何人都可以向我解释如何解决这个问题,或者至少解释为什么它不起作用吗?

编辑:用“g++ -std=gnu++2a main.cpp”编译编辑2:这是输出的错误部分(后面跟着很多很多候选行):

main.cpp: In function 'int main()':
main.cpp:31:25: error: use of deleted function 'constexpr auto operator<=>(const Tree&, const Tree&)'
31 | std::cout << (tmp < tmp) << std::endl;
| ^~~
main.cpp:12:17: note: 'constexpr auto operator<=>(const Tree&, const Tree&)' is implicitly deleted because the default definition would be ill-formed:
12 | friend auto operator<=>(const Tree &a, const Tree &b) = default;
| ^~~~~~~~
main.cpp:12:17: error: no match for 'operator<=>' (operand types are 'std::vector<Tree>' and 'std::vector<Tree>')

最佳答案

您通常不能定义具有推导返回类型的递归函数。为了推断它,您需要已经知道它,所以这是不行的。

用显式返回类型 std::weak_ordering 替换 auto fixes the problem .

关于c++ - 递归宇宙飞船运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67389118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com