Reputation: 21
I created two source files (main.cpp, cal.cpp) and one header file (cal.h).
In cal.cpp,I defined one template function
template <class T>
T add_no(T a, T b){
return (a + b);
}
template add_no<int>;
In cal.h, declared that.
template <class T>
T add_no(T a, T b);
From main.cpp, I called
add_no(2, 5);
but getting the following error.
src\cal.cpp:30:10: error: ISO C++ forbids declaration of 'add_no' with no type [-fpermissive]
template add_no<int>;
^
src\cal.cpp:30:10: error: template-id 'add_no<int>' used as a declarator
src\cal.cpp:30:10: error: 'add_no(T, T)' is not a variable template
* [.pio\build\esp32dev\src\cal.cpp.o] Error 1
Any idea, how I can solve it?
Upvotes: 0
Views: 1240
Reputation: 22152
The correct syntax for explicit function template instantiation requires you to write out the full substituted signature:
template int add_no<int>(int, int);
instead of
template add_no<int>;
This syntax is necessary, because function templates can be overloaded and the compiler must be able to resolve the function template that you are referring to.
If the template argument is deducible from the function parameters, as is the case here, then you can drop the template argument list:
template int add_no(int, int);
Upvotes: 2