功能指针/参考混淆

时间:2014-07-24 20:09:34

标签: c++ pointers reference dbus

这可能有一个简单的答案,但我不知道要搜索什么来找到它..

我正在使用This c ++ dbus包装器来控制音频播放器Clementine(在Linux上)。

略微调整他们的示例客户端显示here(字面上改变一些字符串和类型)我可以很好地控制Clementine。

我的困惑在于方法的定义,用这种表示法完成:

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

当一个主要方法中的所有东西,但当我试图做一个帮助类,我无法弄清楚如何做它时,这很好。这就是我所拥有的:

//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

//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"));
}

测试:

//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;
}

这显然无法编译。

$ 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; 
           ^

如何在助手类中正确定义所有这些代理方法?再一次,我确信这是一个我不太明白的简单概念。

0 个答案:

没有答案