我試圖通過VRPN服務器將我的應用程序連接到OpenViBE。我的應用運行良好,直到我嘗試添加代碼以將我的應用連接到VRPN服務器。函數調用缺少參數列表來創建指針
我的代碼如下所示:
MainWindow.c
代碼:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtUiTools/QUiLoader>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
#include <iostream>
using namespace std;
#include "vrpn_Analog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
currentImage = 0;
labelSize = ui->label_2->size();
createActions();
openFileDialog();
}
void MainWindow::checkChannels()
{
vrpn_Analog_Remote *vrpnAnalog = new vrpn_Analog_Remote("[email protected]");
vrpnAnalog->register_change_handler(0, handle_analog);
}
void VRPN_CALLBACK MainWindow::handle_analog(void* userData, const vrpn_ANALOGCB a)
{
int nbChannels = a.num_channel;
cout << "Analog : ";
for(int i=0; i < a.num_channel; i++)
{
cout << a.channel[i] << " ";
}
cout << endl;
}
MainWindow.h
代碼:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileInfoList>
#include "vrpn_Analog.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void resizeEvent(QResizeEvent *);
private slots:
void openFileDialog();
private:
void checkChannels();
void VRPN_CALLBACK handle_analog(void* userData, const vrpn_ANALOGCB a);
};
#endif // MAINWINDOW_H
有了這個代碼,當我嘗試運行我的應用程序我得到:
error: C3867: 'MainWindow::handle_analog': function call missing argument list; use '&MainWindow::handle_analog' to create a pointer to member
我嘗試編輯由錯誤通知代碼,但我得到另一個錯誤:
error: C2664: 'vrpn_Analog_Remote::register_change_handler' : cannot convert parameter 2 from 'void (__stdcall MainWindow::*)(void *,const vrpn_ANALOGCB)' to 'vrpn_ANALOGCHANGEHANDLER'
There is no context in which this conversion is possible
我四處搜尋,但我沒有找到任何可用的解決方案。
方法checkChannels
,並從這個代碼,所有工作正常handle_analog
我「複製」:
#include <QtCore/QCoreApplication>
#include <iostream>
#include "vrpn_Analog.h"
void VRPN_CALLBACK vrpn_analog_callback(void* user_data, vrpn_ANALOGCB analog)
{
for (int i = 0; i < analog.num_channel; i++)
{
if (analog.channel[i] > 0)
{
std::cout << "Analog Channel : " << i << "/Analog Value : " << analog.channel[i] << std::endl;
}
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/* flag used to stop the program execution */
bool running = true;
/* VRPN Analog object */
vrpn_Analog_Remote* VRPNAnalog;
/* Binding of the VRPN Analog to a callback */
VRPNAnalog = new vrpn_Analog_Remote("[email protected]");
VRPNAnalog->register_change_handler(NULL, vrpn_analog_callback);
/* The main loop of the program, each VRPN object must be called in order to process data */
while (running)
{
VRPNAnalog->mainloop();
}
return 0;
return a.exec();
}
當我在做錯誤?感謝所有回覆。
具體來說,一個函數指針的類型和指針的成員函數是不同的(因爲'this'指針等)。此外,不需要C風格的演員:你可以(應該)做'MainWindow * self = static_cast(userData);',或者如果你被允許使用'auto','auto self = static_cast (userData );'(在回調中調用'this''等價的指針'self''是我見過的常見習慣用法)。你可以調用一個非靜態成員,或者直接使用'self-> myMember'來訪問對象成員。 –