在 OSX 10.10.5 上使用 mongo-cxx-driver 编译 C++ 文件时出错

Error when compiling a C++ file with mongo-cxx-driver on OSX 10.10.5

编译器能够找到所有 driver,但在我尝试使用 << 运算符创建 bsoncxx 文档时给出类型特征错误(不完整类型)。它引用了 c++ 库,所以我没有理由认为 mongo cxx driver 会安装不正确。就在我尝试创建 bsoncxx 文档时,编译器也不会抱怨前面的 mongo 操作。这是我拥有的:

Headers:

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

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/find.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;

代码:

string start;
string end;

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

auto db = conn["test"];

db["shortestpaths"].drop();
document index_spec{};
index_spec << "cost" << "-1"; // THE LINE THAT CAUSES THE ERROR
db["shortestpaths"].create_index(index_spec, {});

auto source = db["topology.src_ip"].find({});
for (auto&& doc : source){
    start = bsoncxx::to_json(doc);
    startVertex = std::stoi(start);

    auto dest = db["topology.dest_ip"].find({});
    for (auto&& field : dest){
        end = bsoncxx::to_json(field);
        destinationVertex = std::stoi(end);
        document filter;
        filter << "source" << start << "dest" << end; // if the line giving the error is commented out, this one gives the same error
        auto dest = db["shortestpaths"].find(filter);
        for (auto&& doc : dest){
            exists = true;
            continue;
        }

        if (startVertex != destinationVertex && !exists){
            YenTopKShortestPathsAlg yenAlg(my_graph, my_graph.get_vertex(startVertex),
                my_graph.get_vertex(destinationVertex));

            while(yenAlg.has_next())
            {
                // TODO: Add mongoDB query here to add the entry
                yenAlg.next()->PrintOut(outFile);
                outFile << '\n';
            }
        }
        exists = false;
    }

生成文件:

CXX=c++
 CFLAGS=-c --std=c++11 -Wall -I/usr/local/mongo-cxx-driver/include/mongocxx/v0.3 -I/usr/local/mongo-cxx-driver/include/libmongoc-1.0 -I/usr/local/mongo-cxx-driver/include/bsoncxx/v0.3 -I/usr/local/mongo-cxx-driver/include/libbson-1.0
 LDFLAGS=-L/usr/local/mongo-cxx-driver/lib -lmongocxx -lbsoncxx pkg-config
 SOURCES=DijkstraShortestPathAlg.cpp Graph.cpp MainP.cpp YenTopKShortestPathsAlg.cpp
 OBJECTS=DijkstraShortestPathAlg.o Graph.o MainP.o YenTopKShortestPathsAlg.o
 EXECUTABLE=algorithm

 all: $(EXECUTABLE)

 $(EXECUTABLE): $(OBJECTS)
    $(CXX) $(OBJECTS) -o $(EXECUTABLE) $(LDFLAGS)

 $(OBJECTS): $(SOURCES)
    $(CXX) $(CFLAGS) -c $(SOURCES)

clean:
    @rm -f $(PROGRAMS) *.o core

当我 运行 make 时,我得到:

In file included from MainP.cpp:5:
In file included from
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/limits:110:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/type_traits:7    94:59: error: 
      incomplete type
          'bsoncxx::v0::builder::stream::value_context<bsoncxx::v0::builder::stream::key_context<bsoncxx::v0::builder::stream::closed_context>
      >' used in type trait expression
    : public integral_constant<bool, __is_base_of(_Bp, _Dp)> {};
                                                          ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:1052:    5: note: 
      in instantiation of template class
      'std::__1::is_base_of<std::__1::ios_base,
          bsoncxx::v0::builder::stream::value_context<bsoncxx::v0::builder::stream::key_context<bsoncxx::v0::builder::stream::closed_context>
      > >' requested here
    is_base_of<ios_base, _Stream>::value,
    ^
/usr/local/mongo-cxx-driver/include/bsoncxx/v0.3/bsoncxx/builder/stream/value_context.hpp:63:86:     note: 
      while substituting deduced template arguments into function template
      'operator<<' [with _Stream =
          bsoncxx::v0::builder::stream::value_context<bsoncxx::v0::builder::stream::key_context<bsoncxx::v0::builder::stream::closed_context>
      >, _Tp = int]
  ...decltype(std::declval<value_context>() << 1 << "str")>::value,
                                            ^
MainP.cpp:67:16: note: in instantiation of template class
          'bsoncxx::v0::builder::stream::value_context<bsoncxx::v0::builder::stream::key_context<bsoncxx::v0::builder::stream::closed_context>
      >' requested here
    index_spec << "cost" << "-1";
               ^
/usr/local/mongo-cxx-driver/include/bsoncxx/v0.3/bsoncxx/builder/stream/value_context.hpp:31:7:     note: 
      definition of
          'bsoncxx::v0::builder::stream::value_context<bsoncxx::v0::builder::stream::key_context<bsoncxx::v0::builder::stream::closed_context>
      >' is not complete until the closing '}'
class value_context {
                    ^

有什么建议吗?

您将 bsoncxx::builder::stream::documentbsoncxx::document 混淆了。

请阅读:Handling BSON in the new driver

这是一个如何使用它的例子:

using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;

auto index_spec = document{} << "cost" << "-1" << finalize;
db["shortestpaths"].create_index(index_spec.view(), {});