2011-07-16 82 views
4

很多時候我在互聯網上找到有用的代碼示例。大約有一半時間他們沒有指定要包含什麼文件,甚至沒有指定包含在命令行中使用-l的庫。你通常如何發現?C++要包含哪些文件和庫?

編輯提示:以下問題已解決。這篇文章的其餘部分可以跳過。

現在,我得到噸的錯誤,而試圖編譯:

53: string Gunzip::gunzip(string& compressed) 
54: { 
55: namespace io = boost::iostreams; 
56: 
57: io::filtering_istream gunzip; 
58: gunzip.push(io::gzip_decompressor()); 
59: std::istringstream in_stream = std::istringstream(compressed); 
60: gunzip.push(in_stream); 
61: 
62: stringstream strstream; 
63: io::copy(gunzip, strstream); 
64: return strstream.str(); 
65: } 

在互聯網上一整天后我想:

option: 3 -L/usr/include/boost 
and: 
8: #include <string> 
9: #include <iostream> 
10: #include <sstream> 

15: #include <boost/iostreams/copy.hpp> 
16: #include <boost/iostreams/device/array.hpp> 
17: #include <boost/iostreams/device/back_inserter.hpp> 
18: #include <boost/iostreams/filter/gzip.hpp> 
19: #include <boost/iostreams/filter/test.hpp> 
20: #include <boost/iostreams/filtering_stream.hpp> 

我的錯誤是:

    from /usr/include/c++/4.5/string:45, 
       from Gunzip.cpp:8: 
/usr/include/c++/4.5/bits/ios_base.h: In copy constructor  ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’: 
In file included from /usr/include/c++/4.5/bits/localefwd.h:43:0, 
/usr/include/c++/4.5/bits/ios_base.h:785:5: error: ‘std::ios_base::ios_base(const  std::ios_base&)’ is private 
/usr/include/c++/4.5/iosfwd:77:11: error: within this context 
/usr/include/c++/4.5/iosfwd: In copy constructor  ‘std::basic_istringstream<char>::basic_istringstream(const  std::basic_istringstream<char>&)’: 
/usr/include/c++/4.5/iosfwd:97:11: note: synthesized method  ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
/usr/include/c++/4.5/streambuf: In copy constructor  ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’: 
/usr/include/c++/4.5/streambuf:773:7: error: ‘std::basic_streambuf<_CharT,  _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>::__streambuf_type&)  [with _CharT = char, _Traits = std::char_traits<char>, std::basic_streambuf<_CharT,  _Traits>::__streambuf_type = std::basic_streambuf<char>]’ is private 
/usr/include/c++/4.5/iosfwd:93:11: error: within this context 
/usr/include/c++/4.5/iosfwd: In copy constructor  ‘std::basic_istringstream<char>::basic_istringstream(const  std::basic_istringstream<char>&)’: 
/usr/include/c++/4.5/iosfwd:97:11: note: synthesized method  ‘std::basic_stringbuf<char>::basic_stringbuf(const std::basic_stringbuf<char>&)’ first  required here 
Gunzip.cpp: In member function ‘std::string Gunzip::gunzip(std::string&)’: 
Gunzip.cpp:59:65: note: synthesized method  ‘std::basic_istringstream<char>::basic_istringstream(const std::basic_istringstream<char>&)’ first required here 
make[2]: Leaving directory `/home/albert/NetBeansProjects/Arb3' 
make[1]: Leaving directory `/home/albert/NetBeansProjects/Arb3' 
make[2]: *** [build/Debug/GNU-Linux-x86/Gunzip.o] Error 1 
make[1]: *** [.build-conf] Error 2 
make: *** [.build-impl] Error 2 

BUILD FAILED (exit value 2, total time: 9s) 

我可以刪除前3個包含獲取其他錯誤我不明白。我不知道哪些錯誤更好。

  1. 此錯誤是否包含?我應該怎麼知道?我在指責包括,因爲迄今爲止所有的錯誤都是由於包括。我不知道basic_ios是什麼。
  2. 你如何找出包含什麼和使用哪些庫?
+1

請發佈完整的錯誤。您發佈的部分忽略了最相關的信息:哪部分代碼導致了錯誤。 –

+0

添加完整的錯誤。我還在代碼中插入了一行。 –

回答

1

簡短的回答是「這取決於」。對於作爲C++標準庫一部分的類/函數,編譯器隨附的手冊頁將列出需要哪些頭和庫;或者您也可以使用在線資源,例如cplusplus.com,MSDNGNU libstdc++ doxygen docs

對於像Boost這樣的東西,你必須看看他們的文檔;然而,顯而易見的問題是'我怎麼知道一個班級是否從提升' - 答案几乎是'谷歌它' - 隨着時間的推移,你會習慣什麼是和不在Boost中。

+0

Boost類通常位於'boost'命名空間的某個地方,因此應該很容易找出它們是否在Boost中。 –

+0

@Martinho:這可以幫助給你一個提示,但是在通常情況下,在網上發佈的片段摘錄人員可能已經完成了「使用命名空間提升」 - 所以只是因爲前面沒有'boost ::'而沒有做'這不意味着它不是來自提升。 – DaveR

0

一般來說,如果找不到所需的庫或不知道如何構建和使用它,找到這些信息的最佳位置是:項目的文檔或維基頁面,論壇或IRC頻道。要查找類/函數屬於哪個項目,可以使用類/函數的名稱進行搜索。

對於上述的代碼段,你需要Boost。大部分你需要做的就是頭文件#include。但某些圖書館需要你建立和鏈接它們。有關說明,請參閱'Getting Started on Microsoft Windows'或'Getting Started on Unix variants (e.g. Linux, MacOS)'。

如果您在Windows機器上工作,你可以從這裏下載預編譯的二進制文件:http://www.boostpro.com/download/

5

您發佈的錯誤實際上是不是有相關的錯誤,或鏈接錯誤這是行:

std::istringstream in_stream = std::istringstream(compressed);

這導致一種遺傳拷貝構造函數的調用被標記爲私有,所以你應該將其替換爲:

std::istringstream in_stream(compressed);

編譯器輸出實際上是在這種情況下,很清楚的。

+0

這兩行代碼執行相同的操作。這裏不使用複製構造函數。 – RocketR

+0

@RocketR:第一個是*複製初始化*,它確實需要複製構造函數是可訪問的。請參閱http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializatio/1051441#1051441 –

+0

看看我的示例http:// www .ideone.com/vj2fH我錯過了什麼? – RocketR

相關問題