error: no matching function for call
to ‘BSTreeNode::BSTreeNode(int, int, NULL,
NULL)’candidates are: BSTreeNode::BSTreeNode(KF, DT&,
BSTreeNode*, BSTreeNode*) [with KF = int, DT = int]
here is how I used it:
BSTreeNode<int, int> newNode(5,9, NULL, NULL) ;
I defined it as follows:
BSTreeNode(KF sKey, DT &data, BSTreeNode *lt, BSTreeNode *rt):key(sKey),dataItem(data), left(lt), right(rt){}
what’s wrong with using my constructor this way?
i’ve been pulling out my hair all night please help me ASAP!!
Non-const references can’t be bound to an rvalue, which is what you’re trying to do with the argument
9for theDT &dataparameter.You’ll either need to pass in a variable that has the value
9, or you’ll need to change the argument (and thedataItemmember if it’s a reference) to beDTtype that gets copied by value into the object. Even if you change the reference to beconstto get rid of the compiler error, you’ll have an issue with the lifetime of the argument that’s passed in if it’s a temporary (it won’t live past the constructor call, so you’ll be left with a dangling reference).Here’s a small example program that demonstrates the problem with binding a const reference in an object to a temporary (an int value returned from
rand()). Note that the behavior is undefined, so it might appear to work under some conditions. I tested this program with debug builds on MSVC 2008 and MinGW 4.5.1:An example run shows: