#include <iostream>
#include <string>
template<int T, int U>
void foo(T a, U b)
{
std::cout << a+b << std::endl;
}
int main() {
foo(2,4);
return 0;
}
我收到以下错误:
error: variable or field 'foo' declared void
error: expected ')' before 'a'
error: expected ')' before 'b'
In function 'int main()': error: 'foo' was not declared in this scope
最佳答案
模板中的 T
和 U
不是类型。您需要将其更改为:
template<typename T, typename U>
void foo(T a, U b) {
}
关于c++ - 声明为 void 的模板函数 - 代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705419/