繁体   English   中英

模板错误:未解决的外部和内部朋友类

[英]Template errors: unresolved externals and inner friend classes

我正在使用模板编写二进制搜索树。 我的想法是,我有一个带有虚拟操作符重载函数的纯抽象基类,用于将其与继承相同类型的其他类进行比较。 此类,或从其继承的任何类,表示BST中的“密钥”。

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

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

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)" (??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

为什么会出现这些错误? 我该如何制止这些?

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

类模板成员函数主体需要位于标头(SearchTree.hpp)中,而不位于.cpp文件中。 有关问题的堆栈溢出规范响应,请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM