Defining and calling a C ++ function of a specialized template

I am currently studying C ++ in depth, and I came across something that has stalled for a couple of hours. Why does this happen when I make a template and then specialize it that I cannot call or define this function for the specialized version? The compiler complains, and I looked at Google for a possible hint that what I am doing wrong, but to no avail. I am very sure that this is something very simple that I do not notice:

template <typename T> class C { }; //specialization to type char template <> class C <char> { public: void echo(); }; //compiler complains here template <> void C <char> :: echo() { cout << "HERE" << endl; } 

error: template-id 'echo <> for' void C :: echo () does not match any template declaration

Demo

+4
source share
1 answer
 //specialization to type char template <> class C <char> { public: void echo(); }; //template<> <----- don't need to mention template<> here void C <char> :: echo() { cout << "HERE\n"; } 

Ps Never say endl when you mean '\n' . What is c ++ iostream endl fiasco?

+7
source

All Articles