+ 1
How to call a template class method?
i've just learned class templates and have began to experiment with them, but the question is: how to i call them correctly? i keep getting three errors: -could not deduce template argument for 'A' -expects 0 arguments - 1 provided - no instance of function template "calculator<C>::addition [with C=int]" matches the argument list my source code: https://code.sololearn.com/cgTc8vXvfHG8
2 Answers
+ 4
If you had just used the type C everywhere, it would have been fine.
But you have used templates further in your class methods, and plus,you have used different names.
Thus, the compiler expects that it is to return a new type and this type must be specified by you before use of that function.
So, to call operation :
user.operation<int>().
And now you may use the returned value.
But, since the type O may not be equal to A,S,M or D in the operations, you will have to change your function calls to:
cout<<"The sum of the two numbers is - "<<addition<O>()<<endl;
And so on for the others.
And Don't forget the braces after the <O>. ( '(' & ')' )
Thus, you need to pass the type O to the arguments A,S,M and D of the templates, as it is no longer possible to determine these implicitly.