0

Why am I getting this error in the following:

template<typename T,typename Y>
class MyContainer
{
        std::vector<T> list;
public:

        void append( T&& elem) ;
};

template<typename T>
void MyContainer<T,int>::append(T&& elem)
{
        list.push_back(elem);
}
int main()
{
}

error: invalid use of incomplete type ‘class >MyContainer’ void MyContainer::append(T&& elem)

I initially thought that, what if someone passed a second template parameter as double, it won't work, so I need to provide a non specialized member function definition first and may be on top of that I can provide partial specialization.

But even after adding the below I still get the same error:

template<typename T, typename Y>
void MyContainer<T,Y>::append(T&& elem)
{
        list.push_back(elem);
}
anurag86
  • 1,635
  • 1
  • 16
  • 31
  • 1
    You can't partially specialize just one member function. You have to partially specialize the whole class; that specialization would define its own member functions, which may or may not match those in the primary template. – Igor Tandetnik Jan 01 '20 at 03:56
  • 1
    Does this answer your question? ["invalid use of incomplete type" error with partial template specialization](https://stackoverflow.com/questions/165101/invalid-use-of-incomplete-type-error-with-partial-template-specialization) or [Template class incomplete specialization](https://stackoverflow.com/questions/13923684/template-class-incomplete-specialization) – walnut Jan 01 '20 at 03:57
  • No the answer to that question not clear to me and i can't ask question on his question, hence asked here separately. – anurag86 Jan 01 '20 at 04:04
  • @anurag86 What exactly is not clear? As the answers and the comment by IgorTandetnik say, you cannot define a partial specialization for a member function of a class template without partially specializing the whole class. There are also more duplicates with similar answers, e.g. [here](https://stackoverflow.com/questions/15374841/c-template-partial-specialization-member-function) and [here](https://stackoverflow.com/questions/1501357/template-specialization-of-particular-members?noredirect=1&lq=1) and more in the linked questions of each. – walnut Jan 01 '20 at 04:11
  • I'm not clear with the work around that he provided `template struct Nested` . yes, i am going through others posts too with no luck. Going over it again – anurag86 Jan 01 '20 at 04:12
  • @walnut Got it now! Thanks – anurag86 Jan 01 '20 at 04:15

0 Answers0