Template multiple definition issue

我的未来我决定 提交于 2019-12-25 08:20:54

问题


I want to use some of my own template classes within my dll project. In order to do so it was suggested here that I still separate my class template declaration from its definition by including the definition of my class's header file (as a .inl file). The class I'm trying to accomplish this with is my own vector class which will just wrap the std::vector class. Example of class setup below:

Vector.h

#pragma once
#include <vector>

namespace BlazeFramework
{
    template<typename type>
    class Vector
    {
    public:
        Vector();
        Vector(int size);
        ~Vector();

    private:
        std::vector<type> _collectionOfItems;
    };
}

#include "Vector.inl"

Vector.inl

#include "Precompiled.h"
#include "Vector.h"

namespace BlazeFramework
{
    template<typename type>
    Vector<type>::Vector()
    {
    }

    template<typename type>
    Vector<type>::Vector(int size) : _collectionOfItems(_collectionOfItems(size, 0))
    {
    }

    template<typename type>
    Vector<type>::~Vector()
    {
    }
}

When I first tried this I got the errors saying "Function Template has already been defined". I figured this was due to my .inl file including the "Vector.h" header at the top so I removed that. However, I'm now getting the errors,

"unrecognizable template declaration/definition".

How do I resolve this issue so I can still separate my class template definitions from their declarations?


回答1:


One solution to keep the definition and implementation templates in separate files is to explicitly instantinate the required templates in the source file. For example:

template class Vector<int>;
template class Vector<float>;

In this case the #include "Vector.inl" from the header should be removed.

If you don't like this approach you could stick to the #include. However, remember that the Vector.inl file should not be compiled as a regular source. If it did, you would get a redefinition of template... kind of error.

Although, keep in mind that in general template classes are best to be compact, simple and designed to be held inside the header file - since this is the hint the compieler uses to generate the actual classes.

I would suggest reading about the topic in the following posts:

  • Splitting templated C++ classes into .hpp/.cpp files--is it possible? - a nice comment about how compiler works in relation to templated classes/functions
  • Why can templates only be implemented in the header file? - another approach of explaining the problem (with code samples)
  • IsoCpp FAQ - Templates - a lot of usefull info regarding templates

Additionally, you probably should look at the initialization list in the constructor - seems to be incorrect.



来源:https://stackoverflow.com/questions/40810526/template-multiple-definition-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!