简体   繁体   English

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

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

I'm writing a binary search tree using templates. 我正在使用模板编写二进制搜索树。 The idea is that I have a pure abstract base class with virtual operator overload functions used for comparing it with other classes which inherit it of the same type. 我的想法是,我有一个带有虚拟操作符重载函数的纯抽象基类,用于将其与继承相同类型的其他类进行比较。 This class, or rather any class which inherits from it, represents the "key" in the BST. 此类,或从其继承的任何类,表示BST中的“密钥”。

A good example would be what I plan to do with this in the beginning, which is add shader sources (in strings, parsed from shader files) to the BST as values, with the key being a type of ShaderComparable , which holds the filename of the shader, and is used for key comparisons within the BST. 一个很好的例子就是我一开始打算做的事情,即将着色器源(以字符串形式,从着色器文件中解析)作为值添加到BST中,键是ShaderComparable的类型,它保存了着色器,用于BST中的关键比较。

The problem is that, while when I was writing the code it would compile fine, as soon as I stick an instance of the BST class in main and try to run it, I get link "unresolved external" errors. 问题是,当我编写代码时,它可以很好地编译,只要我将BST类的实例粘贴在main中并尝试运行它,就会收到“未解决的外部”链接错误。

Code

SearchTree.hpp 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 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
    }

There's more code in the source, I just didn't want to post it all, in hopes to avoid ambiguity. 源代码中有更多代码,我只是不想发布所有代码,以免产生歧义。 The iterator definitions are typically of something along the lines of this: 迭代器定义通常与此类似:

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

etc. 等等

Errors 失误

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

Question

Why am I getting these errors? 为什么会出现这些错误? What can I do to stop these? 我该如何制止这些?

I'm guessing it's because you've made some of your destructors private. 我猜是因为您将某些析构函数设为私有。 Try making them public. 尝试将它们公开。

Class template member function bodies need to be in the header (SearchTree.hpp), not in the .cpp file. 类模板成员函数主体需要位于标头(SearchTree.hpp)中,而不位于.cpp文件中。 See here for the Stack Overflow canonical response on this. 有关问题的堆栈溢出规范响应,请参见此处

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

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