简体   繁体   English

找不到模板功能导致标识符

[英]Template Function results in Identifier not found

I've created this template, and put it at the very top of my .cpp above main() but I am still getting the following 我已经创建了此模板,并将其放在main()上方的.cpp文件的顶部,但仍得到以下内容

error: C3861: 'ConvertNumbertoString': identifier not found. 错误:C3861:'ConvertNumbertoString':找不到标识符。

Here is the template: 这是模板:

template<class T>
string ConvertNumberstoString(T number)
{
    string outPut;
    stringstream convert;

    convert << number;
    outPut = convert.str();

    return outPut;
}

I know this is probably a stupid function to most of you guys, but it's what I need at the moment. 我知道这对大多数人来说可能是愚蠢的功能,但这是我目前需要的。

I can't figure out how to get rid of this error so that I can use it in my program. 我不知道如何摆脱这个错误,所以我可以在程序中使用它。

Any thoughts? 有什么想法吗?

You want to return a string from the function template: 您要从函数模板返回一个字符串:

// In your cpp:

template<class T>
string ConvertNumberstoString(const T &number)
{
    stringstream convert;
    convert << number;
    return convert.str();
}

int main()
{
    string s = ConvertNumberstoString(42);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM