2012-11-28 123 views
0

我有一個函數,我正在傳遞一個變量;然而,變量是一個模板變量。我不知道我應該如何使用模板參數創建函數的具體細節。我甚至不知道我是問正確的問題,但是當我編譯程序,我得到的main.cpp文件中的以下錯誤:在C++函數調用中使用模板定義的變量

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 
+4

'#include「BSTree.cpp」'No no no no no。內聯實現您的模板類。 – GManNickG

+1

你正在用'Node '調用BSTinsert;你的API需要一個簡單的'int'來傳遞。 – Cameron

回答

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

如果我拿走'TNode <>',那麼有14個錯誤,說這... 錯誤:無效轉換從'TNode *'到'int'' –

+0

你可以給我行號或文本對應的錯誤?你也可以添加你的TNode源代碼嗎? – mohaps

+0

所有的錯誤都不是那個錯誤,然而它們都是處理'int'和'TNode的無效轉換'所有錯誤都在'BSTree.cpp'中 行:51,51,52,9,9 ,10,19,19,20,21,30,30,31,32 –