Illegal use of type in template [closed]

2019-09-14 16:19发布

I'm new to templates. I can't figure out what am i doing wrong:

#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;
}

After compiling in VS2013 i got an error: Error 1 error C2275: 'T' : illegal use of this type as an expression

2条回答
混吃等死
2楼-- · 2019-09-14 17:10

maybe you should:

template <typename T>
void inc(T* data)
{
    (*data)++;
}
查看更多
太酷不给撩
3楼-- · 2019-09-14 17:14

*T tries to dereferece the data_type that is why you are getting error.

Please replace the line number 8 of given code snippet with

(*data)++;
查看更多
登录 后发表回答