我多次在互联网上找到有用的代码示例。大约一半的时间他们没有指定要包含的文件,甚至没有指定要在命令行中包含-l的库。你通常如何找到它?
编辑说明:以下问题已解决。可以跳过这篇文章的其余部分。
现在,我在尝试编译时遇到了大量错误:
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个包含以获取其他我不理解的错误。我不知道哪个错误更好。
答案 0 :(得分:5)
您发布的错误实际上不是与包含相关的错误,或链接器错误,它是行:
std::istringstream in_stream = std::istringstream(compressed);
导致对标记为私有的继承复制构造函数的调用,因此您应该将其替换为:
std::istringstream in_stream(compressed);
在这种情况下,编译器输出实际上非常清楚。
答案 1 :(得分:1)
简短的回答是'它取决于'。对于属于C ++标准库的类/函数,编译器附带的手册页将列出所需的头和库;或者您也可以使用cplusplus.com,MSDN或GNU libstdc++ doxygen docs等在线资源。
对于像Boost这样的东西,你必须查看他们的文档;然而,显而易见的问题是“我怎么知道一个班级是否来自提升” - 答案几乎就是“Google it” - 随着时间的推移,你会习惯于Boost中的内容。
答案 2 :(得分:0)
通常,如果您找不到所需的库或不知道如何构建和使用它,找到此类信息的最佳位置是:项目的文档或维基页面,论坛或IRC频道。要查找类/函数所属的项目,可以使用类/函数的名称进行搜索。
对于上面的代码段,您需要Boost。大多数情况下,您需要做的是#include
头文件。但某些库要求您构建和链接它们。有关说明,请参阅“Getting Started on Microsoft Windows”或“Getting Started on Unix variants (e.g. Linux, MacOS)”。
如果您使用的是Windows计算机,可以从此处下载预编译的二进制文件:http://www.boostpro.com/download/