返回指针时输入转换错误

发布于 2025-02-11 00:10:44 字数 589 浏览 1 评论 0原文

ID喜欢返回指针(或引用一个),而不是对我的值的引用。但是我一直遇到转换错误。

我尝试将返回类型更改为节点*,但由于节点是struct,因此它并未将其识别为已知类型。

使用模板时,我很难清楚地了解这些转换误差。我还没有对这些转换找到明确的答案。

error: cannot convert ‘TreeAVL<int>::Node*’ to ‘const int*’ in return
template<class T>
const T* TreeAVL<T>::find(Node* node, const T& element) const {
    if (node == nullptr)
        return nullptr;
    if (element == node->value)
        return &(node->value);
        //return &(node);            <<<< what I want to return if found.
    ...
}

Id like to return a pointer(or reference to one) instead of a reference to my value. But I keep getting conversion errors.

I tried changing the return type to Node* but it doesnt recognize it as a known type since Node is a struct.

I'm having a hard time understanding these conversion erros clearly when using templates. I havent found a clear answer to these conversions yet.

error: cannot convert ‘TreeAVL<int>::Node*’ to ‘const int*’ in return
template<class T>
const T* TreeAVL<T>::find(Node* node, const T& element) const {
    if (node == nullptr)
        return nullptr;
    if (element == node->value)
        return &(node->value);
        //return &(node);            <<<< what I want to return if found.
    ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦开始←不甜 2025-02-18 00:10:44

要了解转换错误,

error: cannot convert ‘TreeAVL<int>::Node*’ to ‘const int*’ in return

您必须查看产生此错误的模板的定义,这将是类似的:

// A function returning const T*
template<class T>
const T* TreeAVL<T>::find(Node* node, const T& element) const {
    // OK, any T* can be a nullptr:
    if (node == nullptr)
        return nullptr;
    // But here the return value is a TreeAVL<T>::Node*, which is not
    // necessarily convertible to any T*
    if (element == node->value)
        return node;
    // ...
}

因此,一旦使用此模板与不兼容的t使用此模板,就会遇到错误,例如示例中的int。如果您想将指针返回到节点,则必须声明函数必须这样做,并且其返回语句必须与此兼容。

另外,如果要避免使用繁琐的打字名称,也可以使用const auto*,或对内联定义并将其指定为const node*

To understand the conversion error,

error: cannot convert ‘TreeAVL<int>::Node*’ to ‘const int*’ in return

You have to look at the definition of the template that produces this error, which would be something like:

// A function returning const T*
template<class T>
const T* TreeAVL<T>::find(Node* node, const T& element) const {
    // OK, any T* can be a nullptr:
    if (node == nullptr)
        return nullptr;
    // But here the return value is a TreeAVL<T>::Node*, which is not
    // necessarily convertible to any T*
    if (element == node->value)
        return node;
    // ...
}

So you get an error as soon as you use this template with an incompatible T, such as int in your example. If you want to return a pointer to a node instead, the function must be declared to do so, and its return statements must all be compatible with that.

Also, if you want to avoid the cumbersome typename, you can also use const auto*, or make the definition inline and specify it as const Node*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文