0

我正在使用模板编写二叉搜索树。这个想法是我有一个带有虚拟运算符重载函数的纯抽象基类,用于将它与继承它的相同类型的其他类进行比较。这个类,或者更确切地说是从它继承的任何类,代表 BST 中的“键”。

一个很好的例子是我一开始就打算这样做,即将着色器源(在字符串中,从着色器文件中解析)作为值添加到 BST,键是类型ShaderComparable,它保存文件名着色器,用于 BST 中的关键比较。

问题是,当我编写代码时,它可以很好地编译,但只要我将 BST 类的一个实例粘贴到 main 中并尝试运行它,就会出现链接“未解决的外部”错误。

代码

搜索树.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;
        };
    };
}

搜索树.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)" (??1?$SearchTree@VShaderComparable@esc@@UShader@2@@esc@@QAE@XZ) 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)" (??0?$SearchTree@VShaderComparable@esc@@UShader@2@@esc@@QAE@XZ) referenced in function _main

问题

为什么我会收到这些错误?我能做些什么来阻止这些?

4

2 回答 2

0

类模板成员函数体需要在标头(SearchTree.hpp)中,而不是在 .cpp 文件中。有关此问题的 Stack Overflow 规范响应,请参见此处

于 2012-05-09T22:44:45.263 回答
0

我猜这是因为您已将一些析构函数设为私有。尝试将它们公开。

于 2012-05-09T18:05:00.537 回答