Hi,
I need to implement a method fun() inside a class classA.
The method fun() should be capable of receiving at most 3 arguments with any data type.
I was following the approach as
class A
{
public:
template<class T1, class T2, class T3>
void fun(T1, T2, T3);
};
template<class T1, class T2, class T3>
void A::fun(T1 t1, T2 t2, T3 t3)
{
}
when the method fun is called from main, it is working only if I'm providing 3 arguments. When I give 2 or 1 arguments it is giving me error as follows.
int main()
{
int i=10;
char c='A';
float f=1.0;
A obj;
obj.fun(i,c,f);// working fine
obj.fun(i,c);// giving an error that fun() does not take 2 arguments
}
can anyone tell me how can we give default value in this case, so that the call for fun() with two arguments can work.
Yes function template cannot have default values. This is what causing an issue here. But, is there any other way of implementation such that a function can receive any data type any default values.
Thanks Bazzy and jsmith.
Well it solves problem in a single file.
But if the templates are in different files then we have linking error as
error LNK2019: unresolved external symbol ....
for the function fun()
If I have header file with class declaration and cpp file for function definition. Then linking error will come from the calling function which is present in some third file.