Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;访问对象_C++_Qt_Leap Motion - Fatal编程技术网

C++ C++;访问对象

C++ C++;访问对象,c++,qt,leap-motion,C++,Qt,Leap Motion,如果有人能帮我,我会很感激的。我不习惯C++。我是新手 我的问题是:我想让一个对象“player”可用于另一个类。我不知道如何存档这个 main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }

如果有人能帮我,我会很感激的。我不习惯C++。我是新手

我的问题是:我想让一个对象“player”可用于另一个类。我不知道如何存档这个

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

return a.exec();
}
我的主要问题是:在引用和实例化时,我做错了什么?leapmotionlistener如何访问相同的播放器对象(我想影响音频)

我得到一个错误:

MusicPlayer\mainwindow.cpp:32: Error:    C2664: 'void  LeapMotionListener::setPlayer(QMediaPlayer &)' : converting from argument  1 'QMediaPlayer *' in 'QMediaPlayer &' not possible
可通过以下链接下载此小项目以快速查看:


提前谢谢

在其他问题中,您试图传递给setPlayer的QMediaPlayer是指针,而不是对对象的引用

您可以这样实例化您的播放器对象:

QMediaPlayer player;
或者,您可以将setPlayer函数签名更改为:

void setPlayer(QMediaPlayer* mplayer);
如果更新函数签名,还需要修复其他函数签名,并在引用播放机时将符号保留在connect()语句之外。

只需

leapMotionListener.setPlayer(*player);

左值引用绑定到对象,而不是指向该对象的指针。为了从指向该对象的指针获取该对象,您需要使用
*

取消对该指针的引用,现在可以执行以下操作:

在MainWindow.h中:

public: 
QMediaPlayer* player;
在MainWindow.cpp中:

player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
在LeapMotionListener.h中:

private:
QMediaPlayer *player;

public: 
void setPlayer(QMediaPlayer *mplayer);
在LeapMotionListener.cpp中:

LeapMotionListener::LeapMotionListener()
: player(0)
{}


void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
     player = mplayer;
}

只是改变这一点对我不起作用。看看我的答案。不过还是谢谢你,谢谢。你的回答不够详细,我对C++的理解不太好。但现在它起作用了。
public: 
QMediaPlayer* player;
player = new QMediaPlayer(this);
leapMotionListener.setPlayer(player);
controller.addListener(leapMotionListener);
private:
QMediaPlayer *player;

public: 
void setPlayer(QMediaPlayer *mplayer);
LeapMotionListener::LeapMotionListener()
: player(0)
{}


void LeapMotionListener::setPlayer(QMediaPlayer *mplayer){
     player = mplayer;
}