locket23 locket23 - 3 months ago 62
C++ Question

c++ invalid conversion from int* to int

I have the following C++ code:

#include <iostream>

using namespace std;
template<class T>
T f(T x, T y)
{
return x+y;
}
int f(int x, int y)
{
return x-y;
}
int main()
{
int *a=new int(3), b(23);
cout<<f(a,b);
return 0;
}


I get these errors: invalid conversion from 'int*' to 'int'

initializing argument 1 of 'int f(int, int)' . What does it mean ?

Thanks

Answer

You need to dereference a in your call to f. This is because a is a pointer to an int, while b is a bare int. Dereferencing a pointer to an int returns an int. This results in the function f being called with two ints.

cout<<f(*a,b);