模板多定义问题 [英] Template multiple definition issue

查看:65
本文介绍了模板多定义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在dll项目中使用一些自己的模板类。为了做到这一点,建议在此处中我仍然通过包括类的头文件(作为.inl文件)的定义,将类模板声明与其定义分开。我正在尝试使用的类是我自己的向量类,它将包装std :: vector类。下面的类设置示例:

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

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()
    {
    }
}

当我第一次尝试此操作时,出现错误消息功能模板已经定义。我认为这是由于我的.inl文件在顶部包含 Vector.h标头所致,因此我将其删除。但是,我现在遇到了错误,

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?

推荐答案

一种将定义和实现模板保存在单独文件中的解决方案是在源文件中显式实例化所需模板。例如:

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>;

在这种情况下, #include Vector.inl

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

如果您不喜欢这种方法,则可以坚持使用 #include 。但是,请记住, Vector.inl 文件不应编译为常规源。如果是这样,您将得到模板的重新定义... 类型的错误。

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.

这篇关于模板多定义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆