I am trying to write a parallel version of wc. I am using TBB library, but unfortunately something is going wrong, and I don't know how to fix the errors.
My code
#include<string>
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<boost/lambda/lambda.hpp>
#include"include/tbb/pipeline.h"
using namespace std;
class Counter
{
private:
public:
unsigned int operator()(std::string const& str){
std::stringstream stream(str);
return std::distance(std::istream_iterator<std::string>(stream), std::istream_iterator<std::string>());
} ;
};
class Reader
{
private:
std::fstream m_file;
string m_line;
public:
Reader(string &path):
m_line(""){
fstream file(path.c_str(), fstream::in);
};
std::string operator()(tbb::flow_control& fc){
if(m_file.eof()){
fc.stop();
m_file.close();
return m_line;
}
getline(m_file,m_line);
return m_line;
}
};
int main(int argc, const char* argv[] ){
unsigned int sum=0;
if(argc!= 3){
cout<<"podaj sciezke!"<<endl;
}else{
string path(argv[1]);
tbb::parallel_pipeline( /*max_number_of_live_token=*/16,
tbb::make_filter<void,string>(tbb::filter::serial_in_order, Reader(path))&
tbb::make_filter<string,unsigned int>(tbb::filter::parallel, Counter())&
tbb::make_filter<unsigned int,void>(tbb::filter::serial_in_order, [&](unsigned int x) {sum+=x;})
);
std::cout<<"word count "<<sum <<endl;
}
return 0;
}`
and compiler output
g
++ -std=gnu++0x my_wc.cpp -o program -Xlinker -rpath -Xlinker lib/ia32/gcc4.4/ -L lib/ia32/gcc4.4/ -ltbb
In file included from my_wc.cpp:7:0:
include/tbb/pipeline.h: In constructor ‘tbb::interface6::internal::filter_node_leaf<T, U, Body>::filter_node_leaf(tbb::filter::mode, const Body&) [with T = void, U = std::basic_string<char>, Body = Reader]’:
include/tbb/pipeline.h:583:63: instantiated from ‘tbb::interface6::filter_t<T, U> tbb::interface6::make_filter(tbb::filter::mode, const Body&) [with T = void, U = std::basic_string<char>, Body = Reader]’
my_wc.cpp:49:80: instantiated from here
include/tbb/pipeline.h:554:77: error: use of deleted function ‘Reader::Reader(const Reader&)’
my_wc.cpp:20:7: error: ‘Reader::Reader(const Reader&)’ is implicitly deleted because the default definition would be ill-formed:
my_wc.cpp:20:7: error: use of deleted function ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’
In file included from my_wc.cpp:3:0:
/usr/include/c++/4.6/fstream:761:11: error: ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:774:11: error: ‘std::basic_iostream<char>::basic_iostream(const std::basic_iostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’
/usr/include/c++/4.6/istream:57:11: error: ‘std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/istream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.6/ios:45:0,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from my_wc.cpp:2:
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/bits/ios_base.h:788:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.6/bits/basic_ios.h:64:11: error: within this context
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’
In file included from /usr/include/c++/4.6/iostream:40:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/ostream:57:11: error: ‘std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/ostream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/ostream:57:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from /usr/include/c++/4.6/iostream:41:0,
from my_wc.cpp:2:
/usr/include/c++/4.6/istream:774:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
In file included from my_wc.cpp:3:0:
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’
/usr/include/c++/4.6/fstream:761:11: error: use of deleted function ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’
/usr/include/c++/4.6/fstream:69:11: error: ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.6/streambuf:782:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const __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.6/fstream:69:11: error: within this context
In file included from my_wc.cpp:7:0:
include/tbb/pipeline.h: In member function ‘void* tbb::interface6::internal::concrete_filter<void, U, Body>::operator()(void*) [with U = std::basic_string<char>, Body = Reader]’:
my_wc.cpp:56:1: instantiated from here
include/tbb/pipeline.h:449:69: error: passing ‘const Reader’ as ‘this’ argument of ‘std::string Reader::operator()(tbb::interface6::flow_control&)’ discards qualifiers [-fpermissive]
include/tbb/pipeline.h: In member function ‘void* tbb::interface6::internal::concrete_filter<T, U, Body>::operator()(void*) [with T = std::basic_string<char>, U = unsigned int, Body = Counter]’:
my_wc.cpp:56:1: instantiated from here
include/tbb/pipeline.h:426:89: error: passing ‘const Counter’ as ‘this’ argument of ‘unsigned int Counter::operator()(const string&)’ discards qualifiers [-fpermissive]
Especially the line
Reader::Reader(const Reader&)’ is implicitly deleted because the default definition would be ill-formed:
is very strange.
This is my first multi thread task, so please give me some advice how can I fix my source code.
The error stems from the fact that tbb::make_filter is attempting to make a copy of the Reader object you pass it. Reader is non-copyable because it holds an std::fstream data member, which is non-copyable. You need to redesign Reader such that it does not hold an std::fstream, or pass an std::reference_wrapper<Reader>.
Related
after using cmake on my opencv build folder, I used cmd to run mingw32-make and got the following error.
In file included from C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:14,
from C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:6:
C:/opencv/sources/modules/core/include/opencv2/core/utils/logtag.hpp:18:5: error: 'LogLevel' does not name a type
LogLevel level;
^~~~~~~~
C:/opencv/sources/modules/core/include/opencv2/core/utils/logtag.hpp:20:38: error: 'LogLevel' has not been declared
inline LogTag(const char* _name, LogLevel _level)
^~~~~~~~
C:/opencv/sources/modules/core/include/opencv2/core/utils/logtag.hpp: In constructor 'cv::utils::logging::LogTag::LogTag(const char*, int)':
C:/opencv/sources/modules/core/include/opencv2/core/utils/logtag.hpp:22:11: error: class 'cv::utils::logging::LogTag' does not have any field named 'level'
, level(_level)
^~~~~
In file included from C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:15,
from C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:6:
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp: At global scope:
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp:20:5: error: 'LogLevel' does not name a type
LogLevel level;
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp:34:48: error: 'LogLevel' has not been declared
LogTagConfig(const std::string& _namePart, LogLevel _level, bool _isGlobal = false,
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp: In constructor 'cv::utils::logging::LogTagConfig::LogTagConfig()':
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp:27:11: error: class 'cv::utils::logging::LogTagConfig' does not have any field named 'level'
, level()
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp: In constructor 'cv::utils::logging::LogTagConfig::LogTagConfig(const string&, int, bool, bool, bool)':
C:\opencv\sources\modules\core\src\utils\logtagconfig.hpp:37:11: error: class 'cv::utils::logging::LogTagConfig' does not have any field named 'level'
, level(_level)
^~~~~
In file included from C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:6:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp: At global scope:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:24:32: error: expected ')' before 'defaultUnconfiguredGlobalLevel'
LogTagConfigParser(LogLevel defaultUnconfiguredGlobalLevel = LOG_LEVEL_VERBOSE);
~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
)
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:40:49: error: 'LogLevel' has not been declared
void parseWildcard(const std::string& name, LogLevel level);
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:41:22: error: 'LogLevel' was not declared in this scope
static std::pair<LogLevel, bool> parseLogLevel(const std::string& s);
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:41:36: error: template argument 1 is invalid
static std::pair<LogLevel, bool> parseLogLevel(const std::string& s);
^
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.hpp:42:33: error: 'LogLevel' has not been declared
static std::string toString(LogLevel level);
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:12:39: error: expected constructor, destructor, or type conversion before '(' token
LogTagConfigParser::LogTagConfigParser(LogLevel defaultUnconfiguredGlobalLevel)
^
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: In member function 'void cv::utils::logging::LogTagConfigParser::parseNameAndLevel(const string&)':
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:105:25: error: request for member 'second' in 'parsedLevel', which is of non-class type 'int'
if (parsedLevel.second)
^~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:108:43: error: request for member 'first' in 'parsedLevel', which is of non-class type 'int'
parseWildcard("", parsedLevel.first);
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:132:21: error: request for member 'second' in 'parsedLevel', which is of non-class type 'int'
if (parsedLevel.second)
^~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:134:59: error: request for member 'first' in 'parsedLevel', which is of non-class type 'int'
parseWildcard(s.substr(0u, colonIdx), parsedLevel.first);
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: At global scope:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:146:65: error: 'LogLevel' has not been declared
void LogTagConfigParser::parseWildcard(const std::string& name, LogLevel level)
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: In member function 'void cv::utils::logging::LogTagConfigParser::parseWildcard(const string&, int)':
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:152:24: error: 'struct cv::utils::logging::LogTagConfig' has no member named 'level'
m_parsedGlobal.level = level;
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:158:24: error: 'struct cv::utils::logging::LogTagConfig' has no member named 'level'
m_parsedGlobal.level = level;
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:164:24: error: 'struct cv::utils::logging::LogTagConfig' has no member named 'level'
m_parsedGlobal.level = level;
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:175:24: error: 'struct cv::utils::logging::LogTagConfig' has no member named 'level'
m_parsedGlobal.level = level;
^~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: At global scope:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:193:11: error: 'LogLevel' was not declared in this scope
std::pair<LogLevel, bool> LogTagConfigParser::parseLogLevel(const std::string& s)
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:193:25: error: template argument 1 is invalid
std::pair<LogLevel, bool> LogTagConfigParser::parseLogLevel(const std::string& s)
^
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: In static member function 'static int cv::utils::logging::LogTagConfigParser::parseLogLevel(const string&)':
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:195:47: error: 'LOG_LEVEL_VERBOSE' was not declared in this scope
const auto falseDontCare = std::make_pair(LOG_LEVEL_VERBOSE, false);
^~~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:195:47: note: suggested alternative: 'CV_LOG_LEVEL_VERBOSE'
const auto falseDontCare = std::make_pair(LOG_LEVEL_VERBOSE, false);
^~~~~~~~~~~~~~~~~
CV_LOG_LEVEL_VERBOSE
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:196:40: error: 'LogLevel' has not been declared
const auto make_parsed_result = [](LogLevel lev) -> std::pair<LogLevel, bool>
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:196:67: error: 'LogLevel' was not declared in this scope
const auto make_parsed_result = [](LogLevel lev) -> std::pair<LogLevel, bool>
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:196:67: note: suggested alternative: 'parseLogLevel'
const auto make_parsed_result = [](LogLevel lev) -> std::pair<LogLevel, bool>
^~~~~~~~
parseLogLevel
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:196:81: error: template argument 1 is invalid
const auto make_parsed_result = [](LogLevel lev) -> std::pair<LogLevel, bool>
^
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: In lambda function:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:198:40: error: cannot convert 'std::pair<int, bool>' to 'int' in return
return std::make_pair(lev, true);
^
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: In static member function 'static int cv::utils::logging::LogTagConfigParser::parseLogLevel(const string&)':
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:209:43: error: 'LOG_LEVEL_SILENT' was not declared in this scope
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:209:43: note: suggested alternative: 'CV_LOG_LEVEL_SILENT'
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
CV_LOG_LEVEL_SILENT
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:216:43: error: 'LOG_LEVEL_DEBUG' was not declared in this scope
return make_parsed_result(LOG_LEVEL_DEBUG);
^~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:216:43: note: suggested alternative: 'CV_LOG_LEVEL_DEBUG'
return make_parsed_result(LOG_LEVEL_DEBUG);
^~~~~~~~~~~~~~~
CV_LOG_LEVEL_DEBUG
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:221:43: error: 'LOG_LEVEL_SILENT' was not declared in this scope
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:221:43: note: suggested alternative: 'CV_LOG_LEVEL_SILENT'
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
CV_LOG_LEVEL_SILENT
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:228:43: error: 'LOG_LEVEL_ERROR' was not declared in this scope
return make_parsed_result(LOG_LEVEL_ERROR);
^~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:228:43: note: suggested alternative: 'CV_LOG_LEVEL_ERROR'
return make_parsed_result(LOG_LEVEL_ERROR);
^~~~~~~~~~~~~~~
CV_LOG_LEVEL_ERROR
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:235:43: error: 'LOG_LEVEL_FATAL' was not declared in this scope
return make_parsed_result(LOG_LEVEL_FATAL);
^~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:235:43: note: suggested alternative: 'CV_LOG_LEVEL_FATAL'
return make_parsed_result(LOG_LEVEL_FATAL);
^~~~~~~~~~~~~~~
CV_LOG_LEVEL_FATAL
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:242:43: error: 'LOG_LEVEL_INFO' was not declared in this scope
return make_parsed_result(LOG_LEVEL_INFO);
^~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:242:43: note: suggested alternative: 'CV_LOG_LEVEL_INFO'
return make_parsed_result(LOG_LEVEL_INFO);
^~~~~~~~~~~~~~
CV_LOG_LEVEL_INFO
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:248:43: error: 'LOG_LEVEL_SILENT' was not declared in this scope
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:248:43: note: suggested alternative: 'CV_LOG_LEVEL_SILENT'
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
CV_LOG_LEVEL_SILENT
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:255:43: error: 'LOG_LEVEL_SILENT' was not declared in this scope
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:255:43: note: suggested alternative: 'CV_LOG_LEVEL_SILENT'
return make_parsed_result(LOG_LEVEL_SILENT);
^~~~~~~~~~~~~~~~
CV_LOG_LEVEL_SILENT
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:271:43: error: 'LOG_LEVEL_WARNING' was not declared in this scope
return make_parsed_result(LOG_LEVEL_WARNING);
^~~~~~~~~~~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:271:43: note: suggested alternative: 'CV_LOG_LEVEL_WARN'
return make_parsed_result(LOG_LEVEL_WARNING);
^~~~~~~~~~~~~~~~~
CV_LOG_LEVEL_WARN
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp: At global scope:
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:282:42: error: 'std::__cxx11::string cv::utils::logging::LogTagConfigParser::toString' is not a static data member of 'class cv::utils::logging::LogTagConfigParser'
std::string LogTagConfigParser::toString(LogLevel level)
^~~~~~~~
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:282:42: error: 'LogLevel' was not declared in this scope
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:282:42: note: suggested alternative: 'parseLogLevel'
std::string LogTagConfigParser::toString(LogLevel level)
^~~~~~~~
parseLogLevel
C:\opencv\sources\modules\core\src\utils\logtagconfigparser.cpp:283:1: error: expected ',' or ';' before '{' token
{
^
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1258: modules/core/CMakeFiles/opencv_core.dir/src/utils/logtagconfigparser.cpp.obj] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:2318: modules/core/CMakeFiles/opencv_core.dir/all] Error 2
mingw32-make: *** [Makefile:182: all] Error 2
Please how do i solve this, and what is LogLevel?.
At first i was using Mingw then i ran into a "mutex" issue.So i changed to Mingw64 to solve it, the I arrived at this problem. Is there a library I'm missing cause I havent found what LogLevel is.
The code example producing the error is (https://godbolt.org/g/WWkTHr):
#include <boost/smart_ptr/atomic_shared_ptr.hpp>
#include <boost/make_shared.hpp>
int main() {
boost::shared_ptr<int> tmp = boost::make_shared<int>();
boost::atomic_shared_ptr<int> test(tmp);
}
The compile error with GCC 8.1 with -Werror=class-memaccess:
In file included from <source>:1:
/opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/atomic_shared_ptr.hpp: In instantiation of 'boost::atomic_shared_ptr<T>::atomic_shared_ptr(boost::shared_ptr<X>) [with T = int]':
<source>:6:43: required from here
/opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/atomic_shared_ptr.hpp:84:20: error: 'void* memcpy(void*, const void*, size_t)' writing to an object of type 'class boost::detail::spinlock' with no trivial copy-assignment [-Werror=class-memaccess]
std::memcpy( &l_, &init, sizeof( init ) );
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/detail/spinlock.hpp:47,
from /opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/detail/spinlock_pool.hpp:25,
from /opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/shared_ptr.hpp:36,
from /opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/atomic_shared_ptr.hpp:16,
from <source>:1:
/opt/compiler-explorer/libs/boost_1_67_0/boost/smart_ptr/detail/spinlock_std_atomic.hpp:27:7: note: 'class boost::detail::spinlock' declared here
class spinlock
^~~~~~~~
cc1plus: some warnings being treated as errors
Compiler returned: 1
The compiler reminds me that I'm using a deleted function .
https://ideone.com/3YAIlA
#include <memory>
using namespace std;
class foo
{
public:
unique_ptr<int> p;
~foo()
{
}
};
int main()
{
foo a, b;
a = move(b);
return 0;
}
compilation info
prog.cpp: In function 'int main()':
prog.cpp:15:4: error: use of deleted function 'foo& foo::operator=(const foo&)'
a = move(b);
prog.cpp:3:7: note: 'foo& foo::operator=(const foo&)' is implicitly deleted because the default definition would be ill-formed:
class foo
prog.cpp:3:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp = std::default_delete<int>]'
In file included from /usr/include/c++/5/memory:81:0,
from prog.cpp:1:
/usr/include/c++/5/bits/unique_ptr.h:357:19: note: declared here
unique_ptr& operator=(const unique_ptr&) = delete;
If I remove the destructor ,my code compiles fine.
https://ideone.com/UFB18P
Because that's what the standard says. When you start adding special member functions, you inhibit the automatic generation of some of the other ones. This is in the same category of rules as how writing a non-default constructor means a default one won't be automatically generated for you.
Add this:
foo& operator=(foo&&) = default;
I'm trying to use the C++14 init-capture feature to move a unique_ptr inside a lambda via capture. For some reason, both gcc and clang refuse to compile my code, insisting that I'm trying to copy a unique_ptr which obviously doesn't work. I thought that avoiding the copy was exactly the point of the init-capture + std::move feature - in fact, passing a unique_ptr seems to be the prime example used by everybody.
What am I doing wrong?
#include <functional>
#include <iostream>
#include <memory>
#include <string>
void runFunc(std::function<void()>&& f) {
auto ff = std::move(f);
ff();
}
int main() {
auto ptr = std::make_unique<std::string>("hello world\n");
runFunc([captured_ptr = std::move(ptr)]() {
std::cout << *captured_ptr;
});
}
Output from gcc:
http://coliru.stacked-crooked.com/a/d91a480b2b6428ac
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
In file included from main.cpp:1:0:
/usr/local/include/c++/5.2.0/functional: In instantiation of 'static void std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::false_type) [with _Functor = main()::<lambda()>; std::false_type = std::integral_constant<bool, false>]':
/usr/local/include/c++/5.2.0/functional:1746:16: required from 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda()>]'
/usr/local/include/c++/5.2.0/functional:2260:19: required from 'std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = main()::<lambda()>; <template-parameter-2-2> = void; _Res = void; _ArgTypes = {}]'
main.cpp:15:6: required from here
/usr/local/include/c++/5.2.0/functional:1710:34: error: use of deleted function 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)'
__dest._M_access<_Functor*>() =
^
main.cpp:13:43: note: 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)' is implicitly deleted because the default definition would be ill-formed:
runFunc([captured_ptr = std::move(ptr)]() {
^
main.cpp:13:43: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::__cxx11::basic_string<char>; _Dp = std::default_delete<std::__cxx11::basic_string<char> >]'
In file included from /usr/local/include/c++/5.2.0/memory:81:0,
from main.cpp:3:
/usr/local/include/c++/5.2.0/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
Output from clang:
http://coliru.stacked-crooked.com/a/4374988d875fcedc
In file included from main.cpp:1:
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1711:10: error: call to implicitly-deleted copy constructor of '(lambda at main.cpp:13:13)'
new _Functor(*__source._M_access<_Functor*>());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:1746:8: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at main.cpp:13:13)>::_M_clone' requested here
_M_clone(__dest, __source, _Local_storage());
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/functional:2260:33: note: in instantiation of member function 'std::_Function_base::_Base_manager<(lambda at main.cpp:13:13)>::_M_manager' requested here
_M_manager = &_My_handler::_M_manager;
^
main.cpp:13:13: note: in instantiation of function template specialization 'std::function<void ()>::function<(lambda at main.cpp:13:13), void>' requested here
runFunc([captured_ptr = std::move(ptr)]() {
^
main.cpp:13:14: note: copy constructor of '' is implicitly deleted because field '' has a deleted copy constructor
runFunc([captured_ptr = std::move(ptr)]() {
^
/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../include/c++/5.2.0/bits/unique_ptr.h:356:7: note: 'unique_ptr' has been explicitly marked deleted here
unique_ptr(const unique_ptr&) = delete;
^
1 error generated.
Because std::function must be copyable:
std::function satisfies the requirements of CopyConstructible and CopyAssignable.
And the constructor in question:
Initializes the target with a copy of f.
The lambda that you are constructing (completely validly) has a unique_ptr member, which makes it noncopyable. If you rewrote runFunc to take an arbitrary functor by value:
template <typename F>
void runFunc(F f) {
auto ff = std::move(f);
ff();
}
it would compile.
I've installed MongoDB on ubuntu 12.10, tested on the mongo shell and it works ok.
Then, i installed cxx-driver from http://dl.mongodb.org/dl/cxx-driver/
Here is a MongoDB client example that i'm trying to build:
#include <cstdlib>
#include <iostream>
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/client/dbclient.h"
#include "mongo/client/dbclientcursor.h"
#include <boost/shared_ptr.hpp>
using namespace mongo;
void run() {
DBClientConnection c;
c.connect("localhost");
boost::shared_ptr<DBClientCursor> cursor =
c.query("test", BSONObj());
while (cursor->more())
std::cout << cursor->next().toString() << std::endl;
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
But this fails with this error:
In file included from /home/lurscher/third_party/usr/local/include/mongo/bson/oid.h:22:0,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:25,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:29,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/misc.h: In member function ‘time_t mongo::Date_t::toTimeT() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/misc.h:96:42: error: ‘verify’ was not declared in this scope
In file included from /home/lurscher/third_party/usr/local/include/mongo/util/hex.h:22:0,
from /home/lurscher/third_party/usr/local/include/mongo/bson/oid.h:23,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:25,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:29,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘void mongo::StringBuilderImpl<Allocator>::appendDoubleNice(double)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:302:28: error: there are no arguments to ‘verify’ that depend on a template parameter, so a declaration of ‘verify’ must be available [-fpermissive]
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:302:28: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:303:33: error: there are no arguments to ‘verify’ that depend on a template parameter, so a declaration of ‘verify’ must be available [-fpermissive]
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::SBNUM(T, int, const char*)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:336:28: error: there are no arguments to ‘verify’ that depend on a template parameter, so a declaration of ‘verify’ must be available [-fpermissive]
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:337:33: error: there are no arguments to ‘verify’ that depend on a template parameter, so a declaration of ‘verify’ must be available [-fpermissive]
In file included from /home/lurscher/third_party/usr/local/include/mongo/bson/oid.h:23:0,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:25,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:29,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/util/hex.h: In function ‘int mongo::fromHex(char)’:
/home/lurscher/third_party/usr/local/include/mongo/util/hex.h:33:23: error: ‘verify’ was not declared in this scope
In file included from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:29:0,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const char* mongo::BSONElement::codeWScopeCode() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:265:70: error: ‘massert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘int mongo::BSONElement::codeWScopeCodeLen() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:272:70: error: ‘massert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const char* mongo::BSONElement::binData(int&) const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:309:39: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘mongo::BinDataType mongo::BSONElement::binDataType() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:328:39: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const char* mongo::BSONElement::regex() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:335:35: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const char* mongo::BSONElement::dbrefNS() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:409:63: error: ‘uassert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const mongo::OID& mongo::BSONElement::dbrefOID() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:414:63: error: ‘uassert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In constructor ‘mongo::BSONElement::BSONElement(const char*, int)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:439:72: error: ‘uassert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘const mongo::BSONElement& mongo::BSONElement::chk(bool) const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:477:83: error: ‘massert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h: In member function ‘int mongo::BSONElement::canonicalType() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:522:21: error: ‘verify’ was not declared in this scope
In file included from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:30:0,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobj.h: In member function ‘void mongo::BSONObj::appendSelfToBufBuilder(mongo::BufBuilder&) const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobj.h:456:31: error: ‘verify’ was not declared in this scope
In file included from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:31:0,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonmisc.h: At global scope:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonmisc.h:173:9: error: ‘auto_ptr’ in namespace ‘std’ does not name a type
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonmisc.h: In member function ‘bool mongo::BSONObjBuilderValueStream::haveSubobj() const’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonmisc.h:171:42: error: ‘_subobj’ was not declared in this scope
In file included from test_mongo.cpp:3:0:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘virtual mongo::BSONObjBuilder& mongo::BSONObjBuilder::append(const mongo::BSONElement&)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:124:30: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘virtual mongo::BSONObjBuilder& mongo::BSONObjBuilder::appendAs(const mongo::BSONElement&, const mongo::StringData&)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:131:30: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘mongo::BSONObjBuilder& mongo::BSONObjBuilder::appendObject(const mongo::StringData&, const char*, int)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:148:29: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘virtual mongo::BSONObj mongo::BSONObjBuilder::obj()’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:554:65: error: ‘massert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘char* mongo::BSONObjBuilder::decouple(int&)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:589:23: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘mongo::Labeler mongo::BSONObjBuilder::operator<<(const mongo::Labeler::Label&)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:629:74: error: ‘massert’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h: In member function ‘void mongo::BSONArrayBuilder::fill(int)’:
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:836:102: error: ‘uassert’ was not declared in this scope
In file included from /home/lurscher/third_party/usr/local/include/mongo/db/jsobj.h:41:0,
from /home/lurscher/third_party/usr/local/include/mongo/client/authentication_table.h:23,
from /home/lurscher/third_party/usr/local/include/mongo/client/dbclientinterface.h:26,
from /home/lurscher/third_party/usr/local/include/mongo/client/connpool.h:23,
from /home/lurscher/third_party/usr/local/include/mongo/client/dbclient.h:32,
from test_mongo.cpp:4:
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h: In member function ‘void mongo::BSONObjBuilderValueStream::endField(const char*)’:
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:359:9: error: ‘_subobj’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h: In member function ‘mongo::BSONObjBuilder* mongo::BSONObjBuilderValueStream::subobj()’:
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:365:13: error: ‘_subobj’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:366:16: error: ‘_subobj’ was not declared in this scope
test_mongo.cpp: In function ‘void run()’:
test_mongo.cpp:15:28: error: conversion from ‘std::auto_ptr<mongo::DBClientCursor>’ to non-scalar type ‘boost::shared_ptr<mongo::DBClientCursor>’ requested
In file included from /home/lurscher/third_party/usr/local/include/mongo/util/hex.h:22:0,
from /home/lurscher/third_party/usr/local/include/mongo/bson/oid.h:23,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:25,
from /home/lurscher/third_party/usr/local/include/mongo/bson/bsonobjbuilder.h:29,
from test_mongo.cpp:3:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘void mongo::StringBuilderImpl<Allocator>::appendDoubleNice(double) [with Allocator = mongo::TrivialAllocator]’:
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:702:42: instantiated from here
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:302:13: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::SBNUM(T, int, const char*) [with T = int, Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:272:53: instantiated from ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::operator<<(int) [with Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’
/home/lurscher/third_party/usr/local/include/mongo/bson/bsonelement.h:469:64: instantiated from here
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:336:13: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::SBNUM(T, int, const char*) [with T = unsigned int, Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:275:53: instantiated from ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::operator<<(unsigned int) [with Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:493:66: instantiated from here
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:336:13: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::SBNUM(T, int, const char*) [with T = long long int, Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:284:55: instantiated from ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::operator<<(long long int) [with Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:693:50: instantiated from here
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:336:13: error: ‘verify’ was not declared in this scope
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h: In member function ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::SBNUM(T, int, const char*) [with T = long long unsigned int, Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’:
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:287:55: instantiated from ‘mongo::StringBuilderImpl<Allocator>& mongo::StringBuilderImpl<Allocator>::operator<<(long long unsigned int) [with Allocator = mongo::TrivialAllocator, mongo::StringBuilderImpl<Allocator> = mongo::StringBuilderImpl<mongo::TrivialAllocator>]’
/home/lurscher/third_party/usr/local/include/mongo/db/../bson/bson-inl.h:776:48: instantiated from here
/home/lurscher/third_party/usr/local/include/mongo/bson/util/builder.h:336:13: error: ‘verify’ was not declared in this scope
Try this...
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h"
using namespace mongo;
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
auto_ptr<DBClientCursor> cursor = c.query("test.test", BSONObj());
while (cursor->more())
std::cout << cursor->next().toString() << std::endl;
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
They can be compiled using...
g++ test.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system -o test
The reason for your code's not working is the order of your includes. You just have to put dbclient.h in front of the the mongo includes. Actually, you only need dbclient.h, as it includes all other necessary headers.
What you additionally need to get this working with a shared_ptr, is a cast, for DBClientConnection::query()returns a std::auto_ptr.
The code below should work fine:
#include "mongo/client/dbclient.h"
using namespace mongo;
void run() {
DBClientConnection c;
c.connect("localhost");
boost::shared_ptr<DBClientCursor> cursor =
(boost::shared_ptr<DBClientCursor>) c.query("test", BSONObj());
while (cursor->more())
std::cout << cursor->next().toString() << std::endl;
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}