Illegal use of type in template [closed]

2019-09-14 17:11发布

问题:

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

回答1:

maybe you should:

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


回答2:

*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)++;