我在过去的几个月里一直在学习C++和使用Terminal。我的代码编译并使用g ++和C++ 11正常运行,但在过去的几天里,它开始出现错误,并且自从编译出现问题。我可以编译和运行的唯一程序依赖于较早的C++标准。为什么std :: stoi和std :: array不能用g ++ C++ 11编译?
我首先得到的有关#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);
}
尝试使用编译心电图$ 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 ::还是使用命名空间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
感谢您能提供任何见解。
尝试运行gcc/clang -v,你会得到什么? 我知道OSX上的支持有点落后于C++ 11,可能有点落后于此:http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport –
@TheFloatingBrain后面跟着gcc也许,铛支持是完全最新的,虽然 – aaronman
感谢您的意见。 'ecg $ clang -v'给出 'Apple LLVM 5.0版(clang-500.2.75)(基于LLVM 3.3svn) 目标:x86_64-apple-darwin12.5.0 线程模型:posix'和'ecg $ gcc - v'给出 '配置为:--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)(基于LLVM 3.3svn) 目标:x86_64-apple-darwin12.5.0 线程模型:posix'这看起来很正常吗? – user2865112