错误:' to_string '不是' std '的成员,在gcc 4.8.2的命令行中编译时,在NetBean

Error: ‘to_string’ is not a member of ‘std’ when compiling in command line with gcc 4.8.2, works in NetBeans?

本文关键字:编译 NetBean 命令行 成员 to string 不是 错误 std gcc      更新时间:2023-10-16

我正在用GoogleTest(1.7.0)测试我的代码。我必须在NetBeans(8.0.2)和直接从命令行编译测试。

NetBeans有GoogleTest集成建议在这里:https://youtu.be/TS2CTf11k1U。我还从命令行(在不同的地方)用包提供的说明构建了GoogleTest。

有问题的代码行是(a和b是双精度):

std::string input = std::to_string(a) + " " + std::to_string(b);
当使用 编译时,

给出error: ‘to_string’ is not a member of ‘std’

g++ -isystem -std=c++0x ~/gtest-1.7.0/include -pthread ~/Task1/test_source.cpp libgtest.a -o test_module1_1

按照GoogleTest文档中的指示。

奇怪的是,当使用NetBeans的"Test"时(如视频中所示),测试可以正确编译和运行。

我正在使用Ubuntu 14.04,我已经尝试更新g++(不是更高的版本),我也尝试用-std=c++11而不是-std=c++0x指定版本。

在NetBeans中有这样的警告:

Library File /usr/include/c++/4.8/string
but there is an unresolved #include <stddef.h>
in included /usr/include/_G_config.h

我发现这可能是因为multilib和我也试图通过检查它是否已安装来修复它。这并不会在编译过程中产生任何警告,所以可能只是IDE混淆了。

但是关于编译错误的真正问题:是否有问题与GoogleTest框架,编译器或我的代码?

p。最好不要建议我不使用to_string,因为这个系统必须对学习c++ 11的学生可用,而且真的不应该是他们不能使用所有应该支持的功能。

我认为您的问题是您向gcc传递命令行参数的顺序

g++ -isystem -std=c++0x ~/gtest-1.7.0/include ...
    ^^^^^^^^^^^^^^^^^^^

-isystem标志应该后跟一个路径,其中包含您希望gcc将其视为系统头文件的头文件。所以-std=c++0x标志被-isystem标志错误地消耗了。您可能希望命令行为

g++ -std=c++0x -isystem ~/gtest-1.7.0/include ...