小编Ahm*_*her的帖子

为什么我的模板功能在此调用中不是有效匹配?

我做了以下代码将数字转换为字符串和反向在代码的注释部分我想使函数类型成为一个模板,认为它将根据上下文获取类型"例如,如果我将它分配给int变量,函数将是类型为int"但这不会发生,编译器会给出错误消息

D:\computer science\project\stringToint.cpp In function 'int main()':
49  25  D:\computer science\project\stringToint.cpp [Error] no matching function for call to 'intstr(const char [10])'
49  25  D:\computer science\project\stringToint.cpp [Error] candidate is:
17  21  D:\computer science\project\stringToint.cpp template<class T> T intstr(std::string)
Run Code Online (Sandbox Code Playgroud)

我认为他们使用stringstream对象时出错了,但如果我指定了函数的类型,我成功地实现了函数,但是这会让我为每种类型编写不同的函数

我想念一些事请帮忙

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

template<typename T> 
string strint (T oty)
{
    string ity;
    stringstream ss;
    ss<<oty;
    ss>>ity;
    return ity;
}
/*
template<typename T> 
T intstr (string oty)
{
    T ity;
    stringstream ss;
    ss<<oty;
    ss>>ity;
    return ity;
}
*/
int intstr …
Run Code Online (Sandbox Code Playgroud)

c++ templates return function

1
推荐指数
1
解决办法
116
查看次数

标签 统计

c++ ×1

function ×1

return ×1

templates ×1