C++ 错误:对‘;的调用没有匹配函数;b屏蔽<;int,int>;::bScreenode(int,int,NULL,NULL)&x2019;-什么';怎么了?

C++ 错误:对‘;的调用没有匹配函数;b屏蔽<;int,int>;::bScreenode(int,int,NULL,NULL)&x2019;-什么';怎么了?,c++,templates,constructor,C++,Templates,Constructor,错误:没有用于调用的匹配函数 到'bstreentode::bstreentode(int,int,NULL, NULL)' 候选项是:BSTreeNode::BSTreeNode(KF,DT&, bScreenode*,bScreenode*)[其中KF=int,DT=int] 下面是我如何使用它的: BSTreeNode<int, int> newNode(5,9, NULL, NULL) ; 这样使用我的构造函数有什么问题 我整晚都在拔头发请尽快帮我 非常量引用不能绑定到右值

错误:没有用于调用的匹配函数 到'bstreentode::bstreentode(int,int,NULL, NULL)'

候选项是:BSTreeNode::BSTreeNode(KF,DT&, bScreenode*,bScreenode*)[其中KF=int,DT=int]

下面是我如何使用它的:

BSTreeNode<int, int> newNode(5,9, NULL, NULL) ;
这样使用我的构造函数有什么问题


我整晚都在拔头发请尽快帮我

非常量引用不能绑定到右值,这是您试图对
DT&data
参数的参数
9
执行的操作

您需要传入一个值为
9
的变量,或者需要将参数(以及
dataItem
成员(如果它是引用))更改为
DT
类型,并按值复制到对象中。即使您将引用更改为
const
,以消除编译器错误,如果传入的参数是临时的,那么传入的参数的生存期也会有问题(它不会超过构造函数调用,因此会留下悬空引用)

下面是一个小的示例程序,它演示了将对象中的常量引用绑定到临时变量(从
rand()
返回的int值)的问题。请注意,该行为未定义,因此在某些条件下可能会起作用。我在MSVC 2008和MinGW 4.5.1上用调试版本测试了这个程序:

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

template <class KF, class DT>
class BSTreeNode {

private:
    KF key;
    DT const& dataItem;
    BSTreeNode* left;
    BSTreeNode* right;

public:
    BSTreeNode(KF sKey, DT const &data, BSTreeNode *lt, BSTreeNode *rt)
        : key(sKey)
        , dataItem(data)
        , left(lt)
        , right(rt)
    {}

    void foo() {
        printf( "BSTreeNode::dataItem == %d\n", dataItem);
    } 
};

BSTreeNode<int, int>* test1()
{
    BSTreeNode<int, int>* p = new BSTreeNode<int, int>(5,rand(), NULL, NULL);

    // note: at this point the reference to whatever `rand()` returned in the
    //  above constructor is no longer valid
    return p;
}


int main()
{
    BSTreeNode<int, int>* p1 = test1();

    p1->foo();

    printf( "some other random number: %d\n", rand());

    p1->foo();
}

非常量引用不能绑定到右值,这是您试图对
DT&data
参数的参数
9
执行的操作

您需要传入一个值为
9
的变量,或者需要将参数(以及
dataItem
成员(如果它是引用))更改为
DT
类型,并按值复制到对象中。即使您将引用更改为
const
,以消除编译器错误,如果传入的参数是临时的,那么传入的参数的生存期也会有问题(它不会超过构造函数调用,因此会留下悬空引用)

下面是一个小的示例程序,它演示了将对象中的常量引用绑定到临时变量(从
rand()
返回的int值)的问题。请注意,该行为未定义,因此在某些条件下可能会起作用。我在MSVC 2008和MinGW 4.5.1上用调试版本测试了这个程序:

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

template <class KF, class DT>
class BSTreeNode {

private:
    KF key;
    DT const& dataItem;
    BSTreeNode* left;
    BSTreeNode* right;

public:
    BSTreeNode(KF sKey, DT const &data, BSTreeNode *lt, BSTreeNode *rt)
        : key(sKey)
        , dataItem(data)
        , left(lt)
        , right(rt)
    {}

    void foo() {
        printf( "BSTreeNode::dataItem == %d\n", dataItem);
    } 
};

BSTreeNode<int, int>* test1()
{
    BSTreeNode<int, int>* p = new BSTreeNode<int, int>(5,rand(), NULL, NULL);

    // note: at this point the reference to whatever `rand()` returned in the
    //  above constructor is no longer valid
    return p;
}


int main()
{
    BSTreeNode<int, int>* p1 = test1();

    p1->foo();

    printf( "some other random number: %d\n", rand());

    p1->foo();
}

非常感谢伯尔先生。这节省了我很多时间。。。当我现在看到它的时候,这是一个愚蠢的错误,但是很容易错过。你能对这个终生问题再多解释一下吗?非常感谢Burr先生。这节省了我很多时间。。。当我现在看到它的时候,这是一个愚蠢的错误,但很容易错过。你能再解释一下关于生命的问题吗?
BSTreeNode::dataItem == 41
some other random number: 18467
BSTreeNode::dataItem == 2293724