c++ - 在模板中非法使用类型

标签 c++ templates template-function

<分区>

我是模板新手。我不知道我做错了什么:

#include "stdafx.h"
#include <iostream>
using namespace std;

template <typename T>
void inc(T* data)
{
    (*T)++;
}

int main()
{
    char x = 'x';
    int b = 1602;

    inc<char>(&x);
    inc<int>(&b);
    cout << x << ", " << b << endl;

    int a = 0;
    cin >> a;
    return 0;
}

在 VS2013 中编译后出现错误: 错误 1 ​​error C2275: 'T' : 非法使用此类型作为表达式

最佳答案

也许你应该:

template <typename T>
void inc(T* data)
{
    (*data)++;
}

关于c++ - 在模板中非法使用类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42479842/

相关文章:

时间:2019-03-08 标签:c++fgetsformatchar*

c++ - 在 DLL 中线程化,其中 DLL 必须在子线程完成之前返回

AngularJS - 如何在事件触发之前等待模板加载

javascript - 带有 grunt-dr-svg-sprites 的 HBS 模板

c++ - C++中的函数模板?

c++ - 从 C 文件调用模板函数

c++ - 设置 QLineEdit 时的 std::out_of_range

c++ - 带有模板关键字的伪析构函数调用

c++ - 为具有特定字段的成员搜索通用范围

c++ - 如何检查带有 NULL 的 glBufferData 是否已完成以及 glMapBuffer 是否不会阻塞?