Member function template of class template can't find definition despite explicit instantiation present. Doesn't link

前端 未结 1 1760
深忆病人
深忆病人 2021-01-26 22:42

Edit: This is not a duplicate of the linked question since I am using explicit instantiation and only a specific type of member functions do not link (others do

1条回答
  •  面向向阳花
    2021-01-26 23:04

    You should use the explicit instantion of the method template template Vector Vector::operator+(const Vector & other) const (for all possible pairs of T and U) in addition to the explicit instantion of the Vector class:

    template Vector Vector::operator+(const Vector & other) const;
    

    Also you may simply move the definition of the Vector::operator+ method to the header file.

    In C++11 the extern template directive was introduced. You may use it in the header file for Vector class (as @StoryTeller suggested in the comments):

    extern template struct Vector;
    

    ...to prevent the compiler from instantiating the Vector class in every translation unit its specializations are used. Of course the same extern template directives may also be used for all Vector::operator+ specializations explicitly instantiated in the .cpp file.

    0 讨论(0)
提交回复
热议问题