尝试为派生类指定类模板时出现链接器错误

时间:2019-04-26 00:04:35

标签: c++ class templates visual-studio-2017 mfc

我是C ++类模板的新手。我在这里做什么错了?

scrollbars.h文件:

namespace Scrollbars
{

template <typename T>
class CScrollbars : public T
{
public:
    CScrollbars();
    ~CScrollbars();
};

};

scrollbars.cpp文件:

#include "Scrollbars.h"

namespace Scrollbars
{

template<class T>
CScrollbars<T>::CScrollbars()
{
    //Constructor
}

template<class T>
CScrollbars<T>::~CScrollbars()
{
    //Destructor
}

};

上面的所有模板都是这样,因此我可以从其他各种类派生它们的基类。例如,在下面的类中,我希望CFancyEditCScrollbars派生,而CRichEditCtrl又从任意类派生,在这种情况下为FancyEdit.h,但可以是任何其他类:

namespace FancyEdit { using namespace Scrollbars; //Derive it from the MFC class CRichEditCtrl class CFancyEdit : public CScrollbars<CRichEditCtrl> { public: CFancyEdit(); ~CFancyEdit(); }; }; 文件:

FancyEdit.cpp

#include "FancyEdit.h" namespace FancyEdit { CFancyEdit::CFancyEdit() { //Constructor } CFancyEdit::~CFancyEdit() { //Destructor } }; 文件:

1>FancyEdit.obj : error LNK2019: unresolved external symbol "public: __cdecl Scrollbars::CScrollbars<class CFancyEdit>::CScrollbars<class CFancyEdit>(void)" referenced in function "public: __cdecl FancyEdit::CFancyEdit::CFancyEdit(void)"
1>FancyEdit.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl Scrollbars::CScrollbars<class CFancyEdit>::~CFancyEdit<class CFancyEdit>(void)" referenced in function "int `public: __cdecl FancyEdit::CFancyEdit::CFancyEdit(void)'::`1'::dtor$0"

它可以编译,但是会给我这些链接器错误:

parent: {
   id: Schema.Object,
   title: String,
   children: []
}

0 个答案:

没有答案