2012-12-29 32 views
2

我已經在Ubuntu 12.10,在蒙戈外殼測試安裝MongoDB的,它工作正常。錯誤構建簡單的MongoDB C++客戶端的例子

然後,我安裝了CXX司機從http://dl.mongodb.org/dl/cxx-driver/

這裏是我試圖建立一個MongoDB的客戶端的例子:

#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; 
} 

但這種失敗,此錯誤:

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 
+0

嘗試重新排序庫: http://stackoverflow.com/questions/45135/linker-order-gcc – semibug

回答

2

試試這個...

#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; 
} 

他們可以使用編譯...

g++ test.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -lboost_system -o test 
+0

不知道爲什麼,這個作品和我的原因例如沒有,但事實是,它的作品,所以感謝 – lurscher

+0

在Ubuntu上,它是:G ++ TEST.CPP -pthread -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -lboost_system -lssl -lcrypto -o測試 –

1

的原因爲您的代碼不工作是你包括的順序。你只需要把dbclient.h放在mongo包含的前面。其實,你只需要dbclient.h,因爲它包括所有其他必要的頭。

你還需要什麼來使用shared_ptr這個工作,是一個演員,爲DBClientConnection::query()返回一個std::auto_ptr

下面的代碼應該很好地工作:

#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; 
}