STOF 函数未在此范围 C++ 中声明

stof function is not declared in this scope c++

本文关键字:C++ 声明 范围 函数 STOF      更新时间:2023-10-16

当我编译代码时,我收到此错误

"stof"未在此范围内声明。

我的代码是

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string str,str2;
cin>>str>>str2;
float a,b;
a = stof(str);  //error
b = stof(str2); //error
cout<<a+b;
return 0;
}

如何解决这个问题?

此外,std::stof只能使用 c++11 版本进行编译,编译应按:

g++ -std=c++11 code.cc

std::stof 位于标准C++标头<string> 中。

使用 dev c++,您需要转到 Project> Project Options (Ctrl +h)> 编译器> 代码生成>语言标准并选择 GNU C++ 11

(不太确定菜单,因为我将语言设置为意大利语,但差不多就是这样)