1

我写了代码来构建二叉搜索树,还写了一个二叉搜索树来测试它是否有效,但是我遇到了很多错误,我调试了很长时间,找不到错误。

#include<vector>

template<typename Key, typename Value>
struct BSTNode{
    Key key;
    Value value;
    BSTNode * left;
    BSTNode * right;
    BSTNode(Key k, Value v, BSTNode *l=NULL, BSTNode *r=NULL) :
    key(k), value(v), left(l), right(r) {}
};

template<typename Key, typename Value>
BSTNode<Key, Value>* min_height(std::vector<int> &v, int left, int right ) {
    if(left<=right){
        int mid=left+ (right-left)/2;
        BSTNode<Key, Value>* node=new BSTNode<Key, Value>(v[mid], v[mid]);
        node->left=min_height(v, left, mid-1 );
        node->right=min_height(v, mid+1, right );
        return node;
    }
}

int main(){
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    BSTNode<int, int>* root=min_height(v, 0, 5);
}

编译器错误:

prog.cpp: In function ‘BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)’:
prog.cpp:48:42: error: no matching function for call to ‘min_height(std::vector<int>&, int&, int&)’
prog.cpp:48:42: note: candidate is:
prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:48:42: note:   couldn't deduce template parameter ‘Key’
prog.cpp:49:41: error: no matching function for call to ‘min_height(std::vector<int>&, int&, int&)’
prog.cpp:49:41: note: candidate is:
prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:49:41: note:   couldn't deduce template parameter ‘Key’
prog.cpp: In function ‘int main()’:
prog.cpp:63:45: error: no matching function for call to ‘min_height(std::vector<int>&, int, int)’
prog.cpp:63:45: note: candidate is:
prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)
prog.cpp:44:22: note:   template argument deduction/substitution failed:
prog.cpp:63:45: note:   couldn't deduce template parameter ‘Key’
prog.cpp:63:22: warning: unused variable ‘root’ [-Wunused-variable]
4

1 回答 1

7

您不能根据返回类型推断模板类型。

你可以是明确的:

BSTNode<int, int>* root = min_height<int, int>(v, 0, 5);

但是,要使其正常工作,您还需要更新您的函数,使其在调用中明确min_height

node->left = min_height<Key, Value>(v, left, mid - 1);
node->right = min_height<Key, Value>(v, mid + 1, right);

但是,从外观上看,min_height总是将整数放入Keyand Valuevector您可以通过使用 a of some使其更通用Type,并通过使用该类型作为KeyandValue类型更明确。

template<typename Type>
BSTNode<Type, Type>* min_height(vector<Type> &v, int left, int right ) {
    if(left<=right) {
        int mid = left + (right - left)/2;
        BSTNode<Type, Type>* node = new BSTNode<Type, Type>(v[mid], v[mid]);
        node->left = min_height(v, left, mid - 1);
        node->right = min_height(v, mid+1, right);
        return node;
    }
}

用法:

BSTNode<int, int>* root = min_height(v, 0, 5);
于 2013-03-11T22:06:49.810 回答