0

This probably has a simple answer, but I do not know what to search for to find it..

I am using This c++ dbus wrapper to control the Audio player Clementine (on linux).

slightly adapting their example client shown here(literally changing a few strings and types) I can control Clementine just fine.

My confusion is in the definition of the methods, done with this notation:

 DBus::MethodProxy<double,double,double>& add_proxy
          = *(object->create_method<double,double,double>("dbuscxx.Quickstart","add")); 

This woks fine when everything in one main method, but when I am trying to make a helper class, and I cannot figure out how to do it. Here is what I have:

//Mpris.hpp
#ifndef MPRIS_HPP
#define MPRIS_HPP

#include <dbus-cxx.h>

using namespace std;
using namespace DBus;

class Mpris
{
public:
    Mpris(string bus_name, string interface_name);

    MethodProxy<string>& identify; 
private:
    Dispatcher::pointer dispatcher;
    Connection::pointer connection;

    ObjectProxy::pointer Root;
    ObjectProxy::pointer TrackList;
    ObjectProxy::pointer Player;
};

#endif

I

//Mpris.cpp

#include "Mpris.hpp"

Mpris::Mpris(string bus_name, string interface_name)
{
    dispatcher = Dispatcher::create();
    connection = dispatcher->create_connection(DBus::BUS_SESSION);

    Root = connection->create_object_proxy(bus_name, "/");
    TrackList = connection->create_object_proxy(bus_name,
            "/TrackList");
    Player = connection->create_object_proxy(bus_name,
            "/Player");

    MethodProxy<string> &identify = *(Root->create_method<string>(interface_name,
                "Identify"));
}

Test:

//MprisTest.cpp

#include "Mpris.hpp"
#include <iostream>
using namespace std;

int main(int arg, char **argv)
{
    Mpris mpris("org.mpris.clementine", "org.freedesktop.MediaPlayer");

    cout << mpris.identify() << endl;

    return 0;
}

This obviously fails to compile.

$ g++ -g -Wall Mpris.cpp MprisTest.cpp -o MprisTest `pkg-config --libs --cflags dbus-cxx-1.0`
Mpris.cpp: In constructor ‘Mpris::Mpris(std::string, std::string)’:
Mpris.cpp:5:1: error: uninitialized reference member in ‘class DBus::MethodProxy<std::basic_string<char> >&’ [-fpermissive]
 Mpris::Mpris(string bus_name, string interface_name)
 ^
In file included from Mpris.cpp:3:0:
Mpris.hpp:15:23: note: ‘DBus::MethodProxy<std::basic_string<char> >& Mpris::identify’ should be initialized
  MethodProxy<string>& identify; 
           ^

How do I properly define all these proxy methods in my helper class? Once again, I'm sure this is a simple concept I just don't quite understand.

lkrasner
  • 112
  • 2
  • 13
  • Your compiler error can be reproduced without nearly as much code as this: http://coliru.stacked-crooked.com/a/58639083daa9d084 – chris Jul 24 '14 at 20:12
  • Thank you. I just don't think I really understand the concept behind this, as a previous java developer, so I'm looking for some clarification – lkrasner Jul 24 '14 at 20:14
  • 1
    Use a [constructor initializer list](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) so you actually initialize it. Having a reference as a data member can be questionable, too. – chris Jul 24 '14 at 20:20
  • Once again, I'm not really sure I understand. I need to declare a large number of these functions. – lkrasner Jul 24 '14 at 20:21
  • 1
    @lkrasner References need to be initialized with a valid value on construction. What does this have to do with the number of such functions being necessary to be declared? – πάντα ῥεῖ Jul 24 '14 at 20:39

0 Answers0