2013-10-10 326 views
5

我在過去的幾個月裏一直在學習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

感謝您能提供任何見解。

+0

嘗試運行gcc/clang -v,你會得到什麼? 我知道OSX上的支持有點落後於C++ 11,可能有點落後於此:http://wiki.apache.org/stdcxx/C%2B%2B0xCompilerSupport –

+0

@TheFloatingBrain後面跟着gcc也許,鐺支持是完全最新的,雖然 – aaronman

+0

感謝您的意見。 '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

回答

4

鐺有一個奇怪的STDLIB,你需要添加下面的標記,當你編譯

-stdlib=libc++

您的代碼段工作在我的Mac與

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

answer描述的問題

相關問題