重新定义模板专业化

问题描述 投票:1回答:1

我一直在寻找我的问题的解决方案,但我找不到它:(所以,我有一个模板:template < int N > class ListOfPolyomines及其为N = 1的特殊化。文件listofpolyomines.h包含在主文件中,一切正常但是当我将listofpolyomines.h包含在shell.h(它在shell.cpp中有实现)和shell.h包含到main.cpp时,那么它就是重定义错误。

我试图在没有shell.cpp的情况下编译它(仅在shell.h中实现)并且它也正常工作。我已经尝试过编译它而不使用spezialization但是使用shell.cpp并且它也在工作。所以模板spezalization和文件shell.cpp是问题所在。每个图书馆当然都有#ifndef #define #endif。而shell不是模板是普通类。

那是我的模板及其方法专长:

template <int N>
class ListOfPolyminoes {
    public:
        ListOfPolyminoes();
        ~ListOfPolyminoes();
        void printPoly();
        ...

    private:
        bool ifPolyExist(Polyminoe<N> temp);
        std::vector< Polyminoe<N> > NPoly;
        void generatePoly(Polyminoe<N-1> poly);
};

template <>
ListOfPolyminoes<1>::ListOfPolyminoes(){
    NPoly.push_back(Polyminoe<1>());
}


// Polyminoe是另一个模板。 ListOfPolymiones具有Polyminoe的向量

这是错误:

In function `__gnu_cxx::new_allocator<Polyminoe<1> >::~new_allocator()': ...`
`[349] multiple definition of ListOfPolyminoes<1>::ListOfPolyminoes()' ...`
`[49] first defined here`

如果有人告诉我我做错了什么以及如何避免这种重新定义,那将是非常好的:)谢谢你提前!

class c++11 templates redefinition
1个回答
0
投票
template <>
inline ListOfPolyminoes<1>::ListOfPolyminoes(){
    NPoly.push_back(Polyminoe<1>());
}

应该有所帮助您没有明确inline您的功能,它是如何在包含您的标头的每个文件中公开定义链接。

© www.soinside.com 2019 - 2024. All rights reserved.