模板函数导致找不到标识符

发布于 2024-12-12 11:01:39 字数 461 浏览 4 评论 0原文

我已经创建了这个模板,并将其放在 main() 上方的 .cpp 的最顶部,但我仍然得到以下内容

错误:C3861:“ConvertNumbertoString”:找不到标识符。

这是模板:

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

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

    return outPut;
}

我知道这对你们大多数人来说可能是一个愚蠢的功能,但这是我目前所需要的。

我不知道如何消除这个错误,以便我可以在我的程序中使用它。

有什么想法吗?

I've created this template, and put it at the very top of my .cpp above main() but I am still getting the following

error: C3861: 'ConvertNumbertoString': identifier not found.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-12-19 11:01:39

您想从函数模板返回一个字符串:

// In your cpp:

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

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文