[英]Why are std::stoi and std::array not compiling with g++ c++11?
在过去的几个月里,我一直在学习C ++和使用终端。 我的代码使用g ++和C ++ 11编译并运行良好,但在最近几天它开始出错并且我在编译时遇到了问题。 我可以编译和运行的唯一程序取决于较旧的C ++标准。
我首先得到的错误与头文件中的#include <array>有关。 不知道为什么会这样,但我通过使用boost / array来解决它。 我无法解决的另一个错误是使用std :: stoi。 array和stoi都应该在C ++ 11标准库中。 我做了以下简单的代码来演示正在发生的事情:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
尝试使用ecg $ g ++编译-o stoi_trial stoi_trial.cpp -std = c ++ 11
array.cpp:13:22:错误:命名空间'std'中没有名为'stoi'的成员; 你是说'ati'吗? int myint = std :: stoi(test); ~~~~~ ^ ~~~ atoi /usr/include/stdlib.h:149:6:注意:'atoi'在这里声明为int atoi(const char *); ^ array.cpp:13:27:错误:没有可行的从'std :: string'(又名'basic_string')转换为'const char *'int myint = std :: stoi(test); ^ ~~~ / usr /include/stdlib.h:149:23:注意:将参数传递给参数int atoi(const char *); 生成^ 2错误。
当使用gcc或clang ++和-std = gnu ++ 11时,我也会在编译时遇到这些错误(我猜他们都依赖于相同的文件结构)。 无论我在代码中指定std ::还是指定使用namespace std,我也会得到相同的错误;
我担心这些问题是因为9月命令行工具通过Xcode更新或因为我安装了boost而导致我的C ++ 11库混乱。 希望有一个简单的解决方案。
我的系统:
配置为: - prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-> dir = / usr / include / c ++ / 4.2.1 Apple LLVM 5.0(clang-500.2.76) )(基于LLVM 3.3svn)目标:x86_64-apple-darwin12.5.0线程模型:posix
感谢您提供的任何见解。
clang有一个奇怪的stdlib,你需要在编译时添加以下标志
-stdlib=libc++
你的代码片段在我的Mac上运行
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
这个答案描述了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.