bunny
bunny

Reputation: 2027

Deducing template arguments

I wrote code to construct binary search tree and also a bfs a binary search tree to test whether it works, but i got a lot of bugs, I debug them for a long time and cannot find the bug.

#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);
}

Compiler error:

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]

Upvotes: 1

Views: 2743

Answers (1)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24133

You can't deduce template types based upon the return type.

You can be explicit though:

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

For this to work though, you also need to update your function to be explicit in its calls to min_height:

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

However, by the looks of it, min_height is always putting integers into Key and Value. You could make it more generic by taking a vector of some Type, and more explicit by using that type as the Key and Value types.

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;
    }
}

Usage:

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

Upvotes: 7

Related Questions