G++ 'stod'未在此范围内声明

g++ 'stod' was not declared in this scope

本文关键字:范围内 声明 stod G++      更新时间:2023-10-16

我正试图在cygwin中使用g++(版本4.8.1)编译以下代码,但它似乎无法使用函数stod():

//test.cpp
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
    string a="1.23";
    cout<<stod(a);
    return 0;
}

我一直得到这个eroor:

test.cpp:9:14: error: 'stod' was not declared in this scope
  cout<<stod(a);

我读到另一个有同样问题的线程。在那里,人们建议使用c++11来解决它。所以我尝试了以下两个命令来编译它,但仍然得到了相同的错误:

g++ -std=c++0x test.cpp -o test
g++ -std=c++11 test.cpp -o test

有人知道这个问题的解决办法吗?

我怎么知道c++11已启用?我需要修改我的代码才能使用它吗?

非常感谢!

它在Coliru上的GCC 4.8中工作(http://coliru.stacked-crooked.com/a/8a68ad0ca64c1bff)还有Clang在我的机器上。可能是你的Cygwin系统不支持这个功能。我建议您只需使用好的旧strtod()即可解决此问题。无论如何,这可能就是stod()在引擎盖下使用的。