0

I have the following Mongo C++ program.

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

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

    int main(int argc, char** argv)
    {    
        // Making a connection to Mongo
        mongocxx::instance instance();
        mongocxx::client client(mongocxx::uri());

        // Access a database
        mongocxx::database db = client["results"];

        return 0;
    }

This line mongocxx::database db = client["results"]; gives the following error message:

illegal index, indirection not allowed.

What exactly am I doing wrong? I'm on Windows 10 64bit using MSBuild to compile.

The full error message is:

Build started 6/12/2017 5:01:21 PM.
Project "F:\Projects\Mongo\attempt_2\testing\build\ALL_BUILD.vcxproj" on node 1 (default targets).
Project "F:\Projects\Mongo\attempt_2\testing\build\ALL_BUILD.vcxproj" (1) is building "F:\Projects\Mongo\attemp
t_2\testing\build\ZERO_CHECK.vcxproj" (2) on node 1 (default targets).
PrepareForBuild:
  Creating directory "x64\Debug\ZERO_CHECK\".
  Creating directory "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\".
InitializeBuildStatus:
  Creating "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
  Checking Build System
  CMake does not need to re-run because F:/Projects/Mongo/attempt_2/testing/build/CMakeFiles/generate.stamp is
  up-to-date.
FinalizeBuildStatus:
  Deleting file "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\unsuccessfulbuild".
  Touching "x64\Debug\ZERO_CHECK\ZERO_CHECK.tlog\ZERO_CHECK.lastbuildstate".
Done Building Project "F:\Projects\Mongo\attempt_2\testing\build\ZERO_CHECK.vcxproj" (default targets).

Project "F:\Projects\Mongo\attempt_2\testing\build\ALL_BUILD.vcxproj" (1) is building "F:\Projects\Mongo\attemp
t_2\testing\build\testapp.vcxproj" (3) on node 1 (default targets).
PrepareForBuild:
  Creating directory "testapp.dir\Debug\".
  Creating directory "F:\Projects\Mongo\attempt_2\testing\build\Debug\".
  Creating directory "testapp.dir\Debug\testapp.tlog\".
InitializeBuildStatus:
  Creating "testapp.dir\Debug\testapp.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
CustomBuild:
  Building Custom Rule F:/Projects/Mongo/attempt_2/testing/src/CMakeLists.txt
  CMake does not need to re-run because F:/Projects/Mongo/attempt_2/testing/build/CMakeFiles/generate.stamp is
  up-to-date.
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64\CL.ex
  e /c /IF:\Projects\Mongo\attempt_2\testing\src\..\..\installed_mongocxx\include\mongocxx\v_noabi /IF:\Project
  s\Mongo\attempt_2\testing\src\..\..\installed_mongocxx\include\bsoncxx\v_noabi /IF:\Softwares\Libraries\Boost
  \boost_1_64_0 /Zi /nologo /W3 /WX- /diagnostics:classic /Od /Ob0 /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debu
  g\"" /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"testapp.dir\D
  ebug\\" /Fd"testapp.dir\Debug\vc141.pdb" /Gd /TP /errorReport:queue F:\Projects\Mongo\attempt_2\testing\src\t
  est.cpp
  test.cpp
F:\Projects\Mongo\attempt_2\testing\src\test.cpp(35): error C2107: illegal index, indirection not allowed [F:\P
rojects\Mongo\attempt_2\testing\build\testapp.vcxproj]
Done Building Project "F:\Projects\Mongo\attempt_2\testing\build\testapp.vcxproj" (default targets) -- FAILED.

Done Building Project "F:\Projects\Mongo\attempt_2\testing\build\ALL_BUILD.vcxproj" (default targets) -- FAILED
.


Build FAILED.

"F:\Projects\Mongo\attempt_2\testing\build\ALL_BUILD.vcxproj" (default target) (1) ->
"F:\Projects\Mongo\attempt_2\testing\build\testapp.vcxproj" (default target) (3) ->
(ClCompile target) ->
  F:\Projects\Mongo\attempt_2\testing\src\test.cpp(35): error C2107: illegal index, indirection not allowed [F:
\Projects\Mongo\attempt_2\testing\build\testapp.vcxproj]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.79
Amani
  • 16,245
  • 29
  • 103
  • 153
  • Well, what are you compiling with? – AresCaelum Jun 12 '17 at 13:53
  • 3
    Long story short. `client` is parsed as a function declaration, that accepts. A function that accepts a function (accepting nothing an returning `mongocxx::uri`) and returns mongocxx::client. – StoryTeller - Unslander Monica Jun 12 '17 at 13:55
  • When you create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), please make sure it is actually *complete* and if possible *verifiable*. That means you need to show us things like included header files. Also please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), which should tell you to include the complete copy-pasted unedited error output, in full. And where the error message comes from (compiler, linker, when running the program, etc.) – Some programmer dude Jun 12 '17 at 13:55
  • @Someprogrammerdude, noted, please see edits. – Amani Jun 12 '17 at 14:03
  • @Eddge, please see edits. – Amani Jun 12 '17 at 14:03
  • Note that `instance` is also being parsed as function – Passer By Jun 12 '17 at 14:09
  • If you remove the parentheses from the `instance` line and change all the parentheses on the `client` line to braces (`{`, `}`), it should work. – Saghm Jun 14 '17 at 17:13
  • @Saghm, I changed this line `mongocxx::instance instance();` to `mongocxx::instance instance;` and this line `mongocxx::client client(mongocxx::uri());` to this `mongocxx::client client{mongocxx::uri{}};` it compiles without errors, but when run the program with mongod running nothing happens. – Amani Jun 14 '17 at 17:29
  • I responded to your other question about this, so let's discuss it there – Saghm Jun 14 '17 at 18:42

0 Answers0