c++ - 声明为 void 的模板函数 - 代码不起作用?

标签 c++ templates

#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

最佳答案

模板中的 TU 不是类型。您需要将其更改为:

template<typename T, typename U>
void foo(T a, U b) {
}

关于c++ - 声明为 void 的模板函数 - 代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705419/

相关文章:

c++ - 在 OpenGL c++ 项目中使用资源中的 .tga (targa) 文件

android - 如何自动化Android项目骨架的创建? (没有安卓工作室)

c++ - 函数中推导参数之前的默认模板参数?

c++ - 必须存在不相关的专业才能编译?

c++ - 如何在qt中调试xcode项目?

javascript - 无法使用请求中的自定义 header 跨域加载 Angular 模板

c++ - 如何简洁地初始化具有多个参数的模板函数?

C++ 模板 : implicit conversion, 没有匹配函数来调用 ctor

c++ - 与非常量引用参数交换

c++ - dynamic_cast 返回 NULL 但它不应该