开发者

c++ What files and libs to include?

Many times I find useful code examples on the Internet. About half of the time they don't specify what files to include or even what libs to include on the command line with -l. How do you usually find that out?

edit note: The problem below has been solved. The remainder of this post can be skipped.

Right now, I'm getting tons of errors while trying to compile:

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: }

After a day on the Internet I'm trying:

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>

The error I have is:

                 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]: L开发者_StackOverflow社区eaving 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)

I can remove the first 3 includes to obtain other errors I don't understand. I don't know which errors are better.

  1. Is this error related to includes? How should I know? I'm blaming the includes because so far all errors were due to includes. I don't know what basic_ios is.
  2. How do you find out what to include and what libs to use?


The error you posted is actually not an include-related error, or a linker error it is the line:

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

that causes a call to an inherited copy constructor that is marked as private, so you should replace it with:

std::istringstream in_stream(compressed);

The compiler output is actually quite clear in this case.


The short answer is 'it depends'. For classes / functions which are part of the C++ standard library the man pages which come with your compiler will list which header(s) and libraries are required; or alternately you can use online resources such as cplusplus.com, MSDN or GNU libstdc++ doxygen docs.

For things like Boost you have to look at their documentation; however the obvious question is 'how do I know if a class is from boost' - to which the answer is pretty much 'Google it' - over time you'll get used to what is and isn't in Boost.


In general if you could not find the required libraries or don't know how to build and use it, the best places to find such information are: the project's docs or wiki page, forum or IRC channel. To find which project the classes/functions belong you can search using the class/function's name.

For the above code snippet you need Boost. Mostly all you need to do is #include the header file. But certain libraries require you to build and link them. See the 'Getting Started on Microsoft Windows' or 'Getting Started on Unix variants (e.g. Linux, MacOS)' for instructions.

If you are working on a Windows machine, you can download the pre-compiled binaries from here: http://www.boostpro.com/download/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜