在C ++中的函数调用中使用模板定义的变量

时间:2012-11-28 00:29:35

标签: c++ function templates tree

我有一个函数,我传入一个变量;但是,变量是模板变量。我不知道如何使用模板参数创建函数的具体细节。我甚至不知道我是否在问正确的问题,但是当我编译程序时,我在main.cpp文件中收到以下错误:

line 17   error: no matching function for call  to 'BSTree<int>::BSTinsert(TNode<int>&)'
line 49   error note: candidates are: void BSTree<T>::BSTinsert(const T&) [with T = int]

我认为我做的一切都是正确的,但这是一个阻碍我继续前进的错误。任何和所有的帮助是受欢迎和赞赏! (我不认为其他两个文件 - treeNode.htreeNode.cpp - 需要显示,但我以前错了。如果是,请知道,我会很乐意发布它们再次感谢你!

main.cpp中:

#include <iostream>
#include <string>

#include "BSTree.cpp"

using namespace std;

int main()
{
    BSTree<int> bt;
    TNode<int> item;

    for(int i=0; i<13; i++)
    {
        cout << "Enter value of item: ";
        cin >> item;
        bt.BSTinsert(item); //this is the line with the error
    }

    cout << "Hello world!" << endl;
    return 0;
}

BSTree.h:

#ifndef BSTREE_H_INCLUDED
#define BSTREE_H_INCLUDED

#include "treeNode.cpp"

template <typename T>
class BSTree
{
    TNode<T> *root;
    void insert(TNode<T> * & r, const T & item);
public:

    BSTree();
    TNode<T>* getRoot();
    void BSTinsert(const T & item);
};

#endif // BSTREE_H_INCLUDED

BSTree.cpp:

#include <iostream>
#include <string>
#include "BSTree.h"

template <typename T>
void BSTree<T>::insert(TNode<T> * & r, const T & item)
{
    if(r == NULL)
        TNode<T> newNode = new TNode<T>(item);
    else if(r == item)
        return;

    else if(r->nodeValue > item)
        insert(r->leftChild, item);
    else if(r->nodeValue > item && r->leftChild == NULL)
    {
        TNode<T> newNode = new TNode<T>(item);
        r->leftChild = newNode;
        newNode->parent = r;
    }

    else if(r->nodeValue < item)
        insert(r->rightChild, item);
    else if(r->nodeValue < item && r->rightChild == NULL)
    {
        TNode<T> newNode = new TNode<T>(item);
        r->rightChild = newNode;
        newNode->parent = r;
    }
}

template <typename T>
BSTree<T>::BSTree()
{
    root = NULL;
}

template <typename T>
TNode<T>* BSTree<T>::getRoot()
{
    return root;
}

template <typename T>
void BSTree<T>::BSTinsert(const T& item) //this is the line the note is referring to
{
    TNode<T> tempRoot = getRoot();
    insert(tempRoot, item);
}

treeNode.cpp:

#include <string>
#include <iostream>
#include "treeNode.h"

using namespace std;

template <typename T>
TNode<T>::TNode()
{
    parent = NULL;
    leftChild = NULL;
    rightChild = NULL;
    nodeValue = 0;
}

template <typename T>
TNode<T>::TNode(const T&item, TNode<T> *left, TNode<T> *right, TNode<T> *par)
{
    parent = par;
    leftChild = left;
    rightChild = right;
    nodeValue = item;
}

template <typename T>
void TNode<T>::printNodeInfo()
{
    cout << "Value: " << nodeValue << endl;
    if(parent != NULL)
        cout << "Parent Value: " << parent << endl;
    if(leftChild != NULL)
        cout << "Left Child Value: " << leftChild << endl;
    if(rightChild != NULL)
        cout << "Right Child Value: " << rightChild << endl;
}

treeNode.h:

#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED

template <typename T>
class TNode
{
public:
    T nodeValue;
    TNode<T> *leftChild, *rightChild, *parent;

    TNode();
    TNode(const T&item, TNode<T> *left = NULL, TNode<T> *right = NULL, TNode<T> *par = NULL);
    void printNodeInfo();

    friend std::istream& operator>>( std::istream& i, TNode<T>& item ){return i;}
};

#endif // TREENODE_H_INCLUDED

1 个答案:

答案 0 :(得分:0)

这会解决您的问题吗?

int main()
{
    BSTree<int> bt;
    int item;

    for(int i=0; i<13; i++)
    {
        cout << "Enter value of item: ";
        cin >> item;
        bt.BSTinsert(item); //this is the line with the error
    }

    cout << "Hello world!" << endl;
    return 0;
}