2016-08-09 10 views
1

C++とMongoDBで何かをやろうとしています。これまでのところ、多くの問題がありましたが、私は引っ張ってきました。MongoDB C++チュートリアルプログラムが失敗します: 'mongocxx :: v_noabi :: logic_error'

は、その後私はこの1つだ:

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 

を率直、イムは希望を失います。これは実行しようとしているImの例です: https://docs.mongodb.com/getting-started/cpp/insert/

コンパイルしたプログラムを実行しようとするとエラーが表示されます。 Imは「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++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx) 

すべてのヘルプは歓迎です!私はC++の経験がほとんどないので、問題になる可能性が少し失われています。

+0

クライアント::デフォルト構築mongocxxのコメントをお読みください:http://mongodb.github.io/mongo-cxx-driver/classmongocxx_1_1client.html#a4fe390cb757d5e594b56e98348038ccf あなたはmongocxxを取るコンストラクタを呼び出す必要があります: :実際にクライアントに接続するためのuri: http://mongodb.github.io/mongo-cxx-driver/classmongocxx_1_1client.html#a4a1f479b088cf658ddfa16f6bb553b95 – acm

+0

また、ドキュメントチームにdocs.mongodb.comのドキュメントを警告しました期限が切れています。一般的に、githubページからリンクされたドキュメントは最新のものであり、ソースコードはより多くのものです。特に、引用している例は、examplesディレクトリ(https://github.com/mongodb/mongo-cxx-driver/tree/master/examples)のコードから派生しています。これらのコードは自動的にビルドされ、 C++ 11ドライバ用のCIシステムであるため、常に正しいはずです。 – acm

+0

ありがとうございました!私は最終的にいくつかの仕事を完了することができます。 – Ankk4

答えて

1

acmが指摘したように、docs.mongodb.comのドキュメントは古くなっています。 Githubの例は正常に動作しています。私はこれを答えとしてマークします。

関連する問題