C++ ';stoi&x27;未在此范围内声明,即使添加-std=c++;0x和自定义函数

C++ ';stoi&x27;未在此范围内声明,即使添加-std=c++;0x和自定义函数,c++,C++,我在使用atoi时遇到问题,我尝试添加-std=c++0x,还尝试使用from\u字符串更改atoi函数 template <typename T> from_string(std::string const & s) { std::stringstream ss(s); T result; ss >> result; // TODO handle errors return result; } 来自字符串的模板(std::

我在使用atoi时遇到问题,我尝试添加
-std=c++0x
,还尝试使用from\u字符串更改atoi函数

template <typename T> from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}
来自字符串的模板(std::string const&s){ 标准::stringstream ss(s); T结果; ss>>result;//TODO处理错误 返回结果; } 但是当我使用这个的时候

C:\Users\Eua\Desktop\cours\main.cpp|382|error: ISO C++ forbids declaration of 'from_string' with no type [-fpermissive]|
C:\Users\Eua\Desktop\cours\main.cpp|392|error: no matching function for call to 'from_string(std::basic_string<char>)'|
C:\Users\Eua\Desktop\cours\main.cpp|392|note: candidate is:|
C:\Users\Eua\Desktop\cours\main.cpp|382|note: template<class T> int from_string(const string&)|
C:\Users\Eua\Desktop\cours\main.cpp|382|note:   template argument deduction/substitution failed:|
C:\Users\Eua\Desktop\cours\main.cpp|392|note:   couldn't deduce template parameter 'T'|
C:\Users\Eua\Desktop\cours\main.cpp|392|error: no matching function for call to 'from_string(std::basic_string<char>)'|
C:\Users\Eua\Desktop\cours\main.cpp|392|note: candidate is:|
C:\Users\Eua\Desktop\cours\main.cpp|382|note: template<class T> int from_string(const string&)|
C:\Users\Eua\Desktop\cours\main.cpp|382|note:   template argument deduction/substitution failed:|
C:\Users\Eua\Desktop\cours\main.cpp|392|note:   couldn't deduce template parameter 'T'|
||=== Build failed: 3 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|
<代码> C:\UsA\\Tabe\ToC\Curs\Meal.CPP 382错误:ISO C++禁止声明“FROSY字符串”,没有类型[fPrime] C:\Users\Eua\Desktop\cours\main.cpp | 392 |错误:调用“from_string(std::basic_string)”时没有匹配函数| C:\Users\Eua\Desktop\cours\main.cpp | 392 |注:候选人为:| C:\Users\Eua\Desktop\cours\main.cpp | 382 |注:模板int来自_字符串(const字符串&)| C:\Users\Eua\Desktop\cours\main.cpp | 382 |注意:模板参数推导/替换失败:| C:\Users\Eua\Desktop\cours\main.cpp | 392 |注意:无法推断模板参数't'| C:\Users\Eua\Desktop\cours\main.cpp | 392 |错误:调用“from_string(std::basic_string)”时没有匹配函数| C:\Users\Eua\Desktop\cours\main.cpp | 392 |注:候选人为:| C:\Users\Eua\Desktop\cours\main.cpp | 382 |注:模板int来自_字符串(const字符串&)| C:\Users\Eua\Desktop\cours\main.cpp | 382 |注意:模板参数推导/替换失败:| C:\Users\Eua\Desktop\cours\main.cpp | 392 |注意:无法推断模板参数't'| ||==生成失败:3个错误,1个警告(0分钟,2秒))===|
如何解决此问题?

您忘记在函数中指定要返回的值
T

#include <iostream>
#include <sstream>
#include <string>

template <typename T>
T from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}

int main()
{
    int x = from_string<int>("1");
    return 0;
}
template <typename T>
/* return type goes here */ from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}
#包括
#包括
#包括
模板
T来自_字符串(std::string const&s){
标准::stringstream ss(s);
T结果;
ss>>result;//TODO处理错误
返回结果;
}
int main()
{
int x=从_字符串(“1”);
返回0;
}
在调试中,我看到
x=1

这对我来说很有效。显示您的代码,以便我们查看错误。

如果您
#包含
并将其限定为属于
std
命名空间(
std::stoi
),则可以使用
stoi

第二个错误是由于没有函数的返回类型造成的。看起来您是想让它返回一个
T

#include <iostream>
#include <sstream>
#include <string>

template <typename T>
T from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}

int main()
{
    int x = from_string<int>("1");
    return 0;
}
template <typename T>
/* return type goes here */ from_string(std::string const & s) {
    std::stringstream ss(s);
    T result;
    ss >> result;    // TODO handle errors
    return result;
}
模板
/*返回类型位于此处*/from_string(std::string const&s){
标准::stringstream ss(s);
T结果;
ss>>result;//TODO处理错误
返回结果;
}

您的函数原型没有定义返回类型。这是个问题

template <typename T> 
   T from_string(std::string const & s) {
   std::stringstream ss(s);
   T result;
    ss >> result;    // TODO handle errors
    return result;
}
模板
T来自_字符串(std::string const&s){
标准::stringstream ss(s);
T结果;
ss>>result;//TODO处理错误
返回结果;
}

from_string的
缺少返回类型
此代码与
stoi
无关…请您帮助指出一些更完整的代码来处理atoi?不要在循环中多次迭代使用此版本的
from_string
。。。它非常非常慢。几十个调用读取一个配置文件,你不会注意到,但是如果你用它来处理一个大数据文件。。。。准备等待。如何添加函数基本\u字符串<代码>'from_string(std::basic_string)'
因为我也得到了错误:调用'from_string(std::basic_string)'没有匹配函数|我有一个函数
静态对键_to_idx(const string&key){size_t pos key.find(“,”;string aux1=key.substr(0,pos);string aux2=key.substr(pos+1);返回make_对(from_string(aux1),from_string(aux2));}
但是当运行时,我得到了
错误:调用'from_string(std::string&')没有匹配的函数。
将string更改为basic_string同样有效。函数原型没有定义返回类型。我为错误说明我的答案而道歉。我是人。是的,这就是为什么让你在事后编辑你的答案以使它更好(我自己一直都需要这个能力)的原因@BenVoigt说得很好