我有以下 C++ 代码:
#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;
}
我收到这些错误:从“int*”到“int”的无效转换
初始化 'int f(int, int)' 的参数 1。这是什么意思 ?
谢谢
最佳答案
您需要在调用f
时取消引用a
。这是因为a
是一个指向 int
的指针,而b
是一个裸int
。取消引用指向 int
的指针会返回 int
。这导致使用两个整数调用函数 f
。
cout<<f(*a,b);
关于C++ 从 int* 到 int 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39064568/