MongoDB C++ 驱动程序 - 错误:尚未声明"mongo::client"

mongodb c++ driver- error: "mongo::client" has not been declared

本文关键字:未声明 mongo client MongoDB 驱动程序 错误 C++      更新时间:2023-10-16

编译并成功安装 mongo-cxx-driver (mongo db c++ driver - 26Compat - 所有测试正常通过(。 目录/usr,所以/usrmongo/client/dbclient.h 存在。

-运行CMD:

g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -lboost_filesystem -lboost_program_options -o tutorial

-文件教程.cpp

#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver
void run() {
  mongo::DBClientConnection c;
  c.connect("localhost");
}
int main() {
    mongo::client::initialize();
    try {
        run();
        std::cout << "connected ok" << std::endl;
    } catch( const mongo::DBException &e ) {
        std::cout << "caught " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

结果 - 错误:

tutorial.cpp: In function ‘int main()’:
tutorial.cpp:11:12: error: ‘mongo::client’ has not been declared

有什么提示吗?

不确定这是否有帮助,但是在使用 apt-get 安装 mongo-dev 包后,我遇到了类似的错误。这不应该在 mongo 2.6 之后完成;它仅适用于 MONGO 2.4 或其他东西。它最终损坏了我的 2.6,所以我不得不清理所有内容,重新安装 mongo,然后根据他们的说明从 github 存储库 https://github.com/mongodb/mongo-cxx-driver 构建 mongo-cxx 驱动程序。

之后 eclipse 仍然为教程提供了错误,但奇怪的是它确实构建了这个东西。我不得不在那里清理调试和发布,最终只得到一个警告,因为包含被搞砸了。所以最后我只是废弃了 eclipse 项目,将教程文件复制到一个新项目中,现在它构建干净。