Chengkun Li
Chengkun Li

Reputation: 133

template functions with multiple templates for specific data type like string?

template <typename Key, typename E>
class BST : public Dictionary<Key, E>
{
    .....
    E FindHelp(BSTNode<Key, E>*, const Key&) const;
    template <typename Key>
    std::string FindHelp(BSTNode<Key, std::string> *root, const Key &k) const;
    ....
};

template <typename Key>
std::string BST<Key, std::string>::FindHelp(BSTNode<Key, std::string> *root, const Key &k) const
{
    if (root == nullptr) return "Not Found!"; // Empty tree
                                   // If smaller than the root go left sub tree
    if (k < root->key()) return FindHelp(root->Left(), k);
    // If bigger than the root go right tree
    if (k > root->key()) return FindHelp(root->Right(), k);
    // If equal to the root return root value
    else return root->Element();
}

I want to add a function dealing with specific data type like std::string, when i wrote my definition like this

error C2244: 'BST::FindHelp': unable to match function definition to an existing declaration

Upvotes: 0

Views: 61

Answers (1)

Nyque
Nyque

Reputation: 391

There is no partial function template specialization. You can only use partial template specialization for class, so you have to partially specialize for BST class first.

template <typename Key, typename E>
class BST : public Dictionary<Key, E>
{
    E FindHelp(BSTNode<Key, E>*, const Key&) const;
};

template<typename Key>
class BST<Key, std::string> : public Dictionary<Key, std::string>
{
    std::string FindHelp(BSTNode<Key, std::string>*, const Key&) const;
};

template <typename Key>
std::string BST<Key, std::string>::FindHelp(BSTNode<Key, std::string> *root, const Key &k) const
{
}

Upvotes: 2

Related Questions