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

时间:2015-11-24 20:04:19

标签: c++ macos mongodb bson

编译器能够找到所有驱动程序,但在我尝试使用<<<<<<<<<<<<<<<<<<<<<运营商。它引用了c ++库,因此我没有理由认为mongo cxx驱动程序安装不正确。当我正在尝试创建bsoncxx文档时,编译器也不会抱怨前面的mongo操作。这就是我所拥有的:

接头:

#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

当我跑步时,我得到:

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 {
                    ^

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您将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(), {});