【问题标题】:Template errors: unresolved externals and inner friend classes模板错误:未解决的外部和内部朋友类
【发布时间】:2012-05-18 05:07:17
【问题描述】:

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

问题

为什么会出现这些错误?我能做些什么来阻止这些?

【问题讨论】:

    标签: templates visual-c++ unresolved-external


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 那么,我不能把它们写在源文件里吗?
      猜你喜欢
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多