2012-05-09 53 views
0

我正在使用模板編寫二叉搜索樹。我的想法是,我有一個純粹的抽象基類,帶有虛擬運算符重載函數,用於將它與其他繼承它的相同類型的類進行比較。這個類或者任何繼承它的類代表了BST中的「關鍵」。模板錯誤:無法解析的外部和內部朋友類

一個很好的例子就是我打算在開始時對此做些什麼,它是將着色器源(在字符串中,從着色器文件解析)添加到BST作爲值,其中鍵是ShaderComparable的類型,其保存着色器的文件名,並用於BST內的關鍵比較。

問題是,雖然當我編寫代碼時,它會很好地編譯,只要我將BST類的實例粘貼在主體中並嘗試運行它,就會得到鏈接「無法解析的外部」錯誤。

代碼

SearchTree.hpp

#pragma once 

#include <stdint.h> 
#include "Comparable.hpp" 

namespace esc 
{ 
    /* 
    * NOTE: K must inherit from type 'Comparable', located in "Comparable.hpp" 
    */ 

    template < typename TComparable, typename TValue > 
    class SearchTree 
    { 
    public: 
     class Iterator; 
    private: 
     struct Node; 
     typedef typename Node TNode; 
    public: 
     SearchTree(void); 
     ~SearchTree(void); 
    public: 
     //TValue find(const TComparable& k); 
     //TValue find(int32_t index); 
     //TValue find(const Iterator& pIter); 

     //Iterator begin(void) const; 
     //Iterator end(void) const; 

     void insert(const TComparable& k, const TValue& v); 
     //void insert(const Iterator& pIter); 

     friend class Iterator; 
    private: 
     int32_t mNodeCount; 
     TNode* mRoot; 
    public: 
     class Iterator 
     { 
     public: 
      Iterator(void); 
      inline TNode* operator->(void) const 
      { return mCurrentNode; } 
     private: 
      ~Iterator(void); 
      int32_t getNumStepsLeftToLeaf(void); 
      int32_t getNumStepsRightToLeaf(void); 
      void tallyDirectionalComparison(int& numLeftTrue, int& numRightTrue, const TComparable& k); 
      void insertLeft(const TComparable& k, const TValue& v); 
      void insertRight(const TComparable& k, const TValue& v); 
      bool isLeafNode(const Node*& a); 
      bool isInternalNode(const Node*& node); 
     private: 
      TNode* mCurrentNode; 
      int32_t mIterPosition; 
      friend class Node; 
     }; 
    private: 
     struct Node 
     { 
     public: 
      int32_t index; 
      TComparable Key; 
      TValue Value; 
     private: 
      TNode* mParent; 
      TNode* mLeftChild; 
      TNode* mRightChild; 
     }; 
    }; 
} 

SearchTree.cpp

template < typename TComparable, typename TValue > 
    SearchTree< TComparable, TValue >::SearchTree(void) 
     : mRoot(NULL), 
      mPosition(0), 
      mNodeCount(0) 
    {} 

    template < typename TComparable, typename TValue > 
    SearchTree< TComparable, TValue >::~SearchTree(void) 
    { 
     //TODO 
    } 

有源更多的代碼,我只是不想發佈這一切,希望避免含糊不清。迭代器定義的一般沿此線的東西:

template < typename TComparable, typename TValue > 
void SearchTree< TComparable, TValue >::Iterator::insertRight(const TComparable& k, const TValue& v) 

錯誤

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall esc::SearchTree<class esc::ShaderComparable,struct esc::Shader>::~SearchTree<class esc::ShaderComparable,struct esc::Shader>(void)" ([email protected]@[email protected]@[email protected]@@[email protected]@[email protected]) referenced in function _main 

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall esc::SearchTree<class esc::ShaderComparable,struct esc::Shader>::SearchTree<class esc::ShaderComparable,struct esc::Shader>(void)" ([email protected]@[email protected]@[email protected]@@[email protected]@[email protected]) referenced in function _main 

問題

爲什麼會出現這些錯誤?我能做些什麼來阻止這些?

回答

0

我猜這是因爲你已經使一些你的析構函數是私有的。試着讓它們公開。

0

類模板成員函數體需要在標題(SearchTree.hpp)中,而不是在.cpp文件中。請參閱here瞭解堆棧溢出規範響應。

+0

所以,我不能在源文件中寫入它們? – zeboidlund

相關問題