MongoDB C++;辅导课程失败:';mongocxx::v_noabi::逻辑错误'; 我试图用C++和MunGDB来完成一些事情。到目前为止,出现了无数的问题,但我已经渡过了难关

MongoDB C++;辅导课程失败:';mongocxx::v_noabi::逻辑错误'; 我试图用C++和MunGDB来完成一些事情。到目前为止,出现了无数的问题,但我已经渡过了难关,c++,mongodb,c++11,mongo-cxx-driver,C++,Mongodb,C++11,Mongo Cxx Driver,然后我得到了这个: terminate called after throwing an instance of 'mongocxx::v_noabi::logic_error' what(): invalid use of default constructed or moved-from mongocxx::client object Aborted 坦率地说,我正在失去希望。这是我尝试运行的示例: . 尝试运行已编译程序时出现错误。我能够很好地编译和运行“hellomongo”示

然后我得到了这个:

terminate called after throwing an instance of 'mongocxx::v_noabi::logic_error'
  what():  invalid use of default constructed or moved-from mongocxx::client object
Aborted
坦率地说,我正在失去希望。这是我尝试运行的示例: .

尝试运行已编译程序时出现错误。我能够很好地编译和运行“hellomongo”示例,因此至少部分驱动程序安装正确

我的代码:

#include <chrono>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/types.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;

int main(int, char**)   
{

    mongocxx::instance inst{};
    mongocxx::client conn{};

    auto db = conn["test"];

    bsoncxx::document::value restaurant_doc =
        document{} << "address" << open_document << "street"
                   << "2 Avenue"
                   << "zipcode"
                   << "10075"
                   << "building"
                   << "1480"
                   << "coord" << open_array << -73.9557413 << 40.7720266 << close_array
                   << close_document << "borough"
                   << "Manhattan"
                   << "cuisine"
                   << "Italian"
                   << "grades" << open_array << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "A"
                   << "score" << 11 << close_document << open_document << "date"
                   << bsoncxx::types::b_date { std::chrono::system_clock::time_point {
    std::chrono::milliseconds { 12323 } } } << "grade"
                   << "B"
                   << "score" << 17 << close_document << close_array << "name"
                   << "Vella"
                   << "restaurant_id"
                   << "41704620" << finalize;

    // We choose to move in our document here, which transfers ownership to insert_one()
    auto res = db["restaurants"].insert_one(std::move(restaurant_doc));
}

感谢您的帮助!我对C++的经验很小,所以我对可能出现的问题有点迷惑。p> 正如acm指出的,docs.mongodb.com上的文档已经过时。Github示例运行良好。我会将此标记为已回答。

请阅读默认构造的mongocxx::client的注释:您需要调用使用mongocxx::uri实际连接客户端的构造函数:此外,我已通知文档团队docs.mongodb.com上的文档已过期。一般来说,从github页面链接的文档更为更新,源代码更为更新。特别是,您引用的示例来自examples目录()中的代码,这些代码是作为C++11驱动程序的CI系统的一部分自动生成和运行的,因此它们应该总是正确的。谢谢!我终于可以完成一些工作了。
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)