2

我已经在 ubuntu 12.10 上安装了 MongoDB,在 mongo shell 上进行了测试,它工作正常。

然后,我从http://dl.mongodb.org/dl/cxx-driver/安装了 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
4

2 回答 2

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
于 2012-12-29T15:05:08.380 回答
1

您的代码不起作用的原因是您包含的顺序。你只需要把dbclient.h前面的 mongo 包括进来。实际上,您需要dbclient.h,因为它包含所有其他必要的标题。

shared_ptr为了DBClientConnection::query()返回 a ,您还需要使用 a进行强制转换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;
}
于 2015-03-12T15:57:50.377 回答