Search code examples
classc++11templatesredefinition

Redefinition of template specialization


I've been looking solution for my problem, but i couldn't find it :( So, I have a template : template < int N > class ListOfPolyomines and its spezialization for N = 1. The file listofpolyomines.h is included to main file and everything is working fine. But when I included listofpolyomines.h to shell.h (which has implementation in shell.cpp) and shell.h to main.cpp, then it is redefinition error.

I've tried to compile it without shell.cpp ( implementation only in shell.h ) and it was working fine too. I've tried also compile it without the spezialization but with shell.cpp and it was working too. So the template spezalization and file shell.cpp are the problem. Every library has of course #ifndef #define #endif. And shell is not a template is a normal class.

That's my template and its method specialization:

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 is another template. ListOfPolymiones has vector of Polyminoe

That's the error:

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

It would be really nice, if someone tell me what I am doing wrong and how to avoid this redefinition :) Thank you in advance!


Solution

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

    should help. You don't explicitly inline your function that's how it gets linkage-publicly defined in every file that includes your header.