Illegal use of type in template [closed]

帅比萌擦擦* 提交于 2019-12-12 01:36:41

问题


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


来源:https://stackoverflow.com/questions/42479842/illegal-use-of-type-in-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!