Here is my simple program. I tried forcing the static_cast
so that the compiler knows that U
is nothing but int
. However, still, the template cannot instantiate the int version of my function Add
.
In the future, I want to extend this template so that U
and V
are of different types and my program still works.
However, currently, even the same type is not working. What am I missing here?
What is the reason for the compilation error?
#include <iostream>
using namespace std;
template <typename U>
class Addable
{
U x;
public:
Addable(U val) : x{ val } { cout << "Constructor called" << endl; }
template <typename T>
U add(T y);
};
U Addable::add(T y)
{
x += y;
return x;
}
int main()
{
Addable<int> A{ 10 };
int x = A.add(static_cast<int>(10));
cout << x;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire