gpt4 book ai didi

c++ - 为什么 std::stoi 和 std::array 不能用 g++ c++11 编译?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:52 26 4
gpt4 key购买 nike

过去几个月我一直在学习 C++ 和使用终端。我的代码使用 g++ 和 C++11 编译和运行良好,但在过去的几天里它开始出错,从那以后我在编译时遇到了问题。我唯一可以编译和运行的程序依赖于旧的 C++ 标准。

我首先得到的错误与头文件中的#include 有关。不知道为什么会这样,但我通过使用 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: error: no member named 'stoi' in namespace 'std'; did you mean 'atoi'? int myint = std::stoi(test); ~~~~~^~~~ atoi /usr/include/stdlib.h:149:6: note: 'atoi' declared here int atoi(const char *); ^ array.cpp:13:27: error: no viable conversion from 'std::string' (aka 'basic_string') to 'const char *' int myint = std::stoi(test); ^~~~ /usr/include/stdlib.h:149:23: note: passing argument to parameter here int atoi(const char *); ^ 2 errors generated.

在使用 gcc 或 clang++ 和 -std=gnu++11 时,我在编译时也会遇到这些错误(我猜它们都依赖于相同的文件结构)。无论我在代码中指定 std::还是指定 using namespace std;,我也会遇到同样的错误;

我担心这些问题的出现是因为 9 月通过 Xcode 更新了命令行工具,或者是因为我安装了 boost,这不知何故弄乱了我的 C++11 库。希望有一个简单的解决方案。

我的系统:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn) Target: x86_64-apple-darwin12.5.0 Thread model: posix

感谢您提供的任何见解。

最佳答案

clang有一个奇怪的stdlib,编译的时候需要加上下面的flag

-stdlib=libc++

你的代码片段可以在我的 mac 上运行

g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test

answer描述问题

关于c++ - 为什么 std::stoi 和 std::array 不能用 g++ c++11 编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19285775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com