我试图在Qt中的个人项目中使用TeamSpeak SDK,当我主要使用这段代码时,它工作得很好
它没有问题地编译。问题是当我在Qt主窗口中使用它时:
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <teamspeak/public_definitions.h>
#include <teamspeak/public_errors.h>
#include <teamspeak/serverlib_publicdefinitions.h>
#include <teamspeak/serverlib.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void onClientConnected(uint64 serverID, anyID clientID, uint64 channelID, unsigned int* removeClientError);
ServerLibFunctions funcs; // it's a struct that have pointer fucntions
};
#endif // MAINWINDOW_HMainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
funcs.onClientConnected = onClientConnected; // error here
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onClientConnected(uint64 serverID, anyID clientID, uint64 channelID, unsigned int* removeClientError) {
char* clientName;
unsigned int error;
/* Query client nickname */
if ((error = ts3server_getClientVariableAsString(serverID, clientID, CLIENT_NICKNAME, &clientName)) != ERROR_ok) {
char* errormsg;
if (ts3server_getGlobalErrorMessage(error, &errormsg) == ERROR_ok) {
printf("Error querying client nickname: %s\n", errormsg);
ts3server_freeMemory(errormsg);
}
return;
}
printf("Client '%s' joined channel %llu on virtual server %llu\n", clientName, (unsigned long long) channelID, (unsigned long long)serverID);
/* Example: Kick clients with nickname "BlockMe from server */
if (!strcmp(clientName, "BlockMe")) {
printf("Blocking bad client!\n");
*removeClientError = ERROR_client_not_logged_in; /* Give a reason */
}
}我对Mainwindow.cpp中的错误和错误进行了注释:
无法将'MainWindow::onClientConnected‘从类型'void“(MainWindow::)(uint64,anyID,uint64,无符号int*) {aka (MainWindow:)(长无符号int,短无符号int,长无符号int,无符号int*)}”转换为'void ()(uint64,anyID,uint64,unsigned int) {又名void ()(long unsigned int,short unsigned int,long long unsigned int,long long unsigned int,long long unsigned int,unsigned )}’=;^
我使用的是Windows 10 Mingw编译器QT5.6.1
如何在oop c++中使用此回调函数?
发布于 2016-09-30 21:45:01
我解决了在Qt中使用TeamSpeak的问题,我在main.cpp中初始化了服务器,并在结构中分配了所有的回调函数,现在我可以在主窗口中使用服务器的任何函数,例如,如果我想在文本编辑中显示通道,我可以在任何c++类或Qt对话框中使用它的函数,并且可以在没有问题的情况下调用main.cpp代码。
// put the fucntion of the call back here
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
char *version;
short abort = 0;
uint64 serverID;
unsigned int error;
int unknownInput = 0;
uint64* ids;
int i;
struct ServerLibFunctions funcs;
/* Initialize all callbacks with NULL */
memset(&funcs, 0, sizeof(struct ServerLibFunctions));
funcs.onClientConnected = onClientConnected;
funcs.onClientDisconnected = onClientDisconnected;
funcs.onClientMoved = onClientMoved;
funcs.onChannelCreated = onChannelCreated;
funcs.onChannelEdited = onChannelEdited;
funcs.onChannelDeleted = onChannelDeleted;
funcs.onServerTextMessageEvent = onServerTextMessageEvent;
funcs.onChannelTextMessageEvent = onChannelTextMessageEvent;
funcs.onUserLoggingMessageEvent = onUserLoggingMessageEvent;
funcs.onClientStartTalkingEvent = onClientStartTalkingEvent;
funcs.onClientStopTalkingEvent = onClientStopTalkingEvent;
funcs.onAccountingErrorEvent = onAccountingErrorEvent;
funcs.onCustomPacketEncryptEvent = nullptr;
funcs.onCustomPacketDecryptEvent = nullptr;
if((error = ts3server_initServerLib(&funcs, LogType_FILE | LogType_CONSOLE | LogType_USERLOGGING, NULL)) != ERROR_ok) {
char* errormsg;
if(ts3server_getGlobalErrorMessage(error, &errormsg) == ERROR_ok) {
printf("Error initialzing serverlib: %s\n", errormsg);
ts3server_freeMemory(errormsg);
}
return 1;
}
MainWindow w;
w.show();
return a.exec();
// use any function to edit server in any c++ class as show chan add chancel and so on
}使用所有函数来编辑任何c++类中的服务器,它可以工作,我认为我们不需要在main中多次初始化服务器,也不需要在类中初始化它,如果我想使用Qt生成一个VoIP,我们只需要服务器编辑功能,如果有更好的答案,请张贴感谢编辑。
另一种解决方案是使用main中的回调函数初始化结构,并在主窗口或构造函数中的任何c++类中传递它,并使用它初始化TeamSpeak的服务器库。
发布于 2016-09-30 20:10:41
您的问题可以用下面的简单代码来模拟。根据我的理解,您要分配的函数是一个特定于类的成员。因此,当您试图分配时,编译器将其视为签名不匹配。您可能需要在非类成员中编写所需的功能,然后进行分配。(除非有办法允许这种分配)。
将函数输入的结构作为函数类的朋友,如下所示。
#include <iostream>
// common namespace
using namespace std;
//Structure with function pointers.
struct funcPtrStruct
{
public:
void (*func)(int a, float b);
};
//The non member function that address your functionality
void testFuncOne(int a ,float b)
{
//Do something
}
//Class implementation
class Test
{
public:
Test()
{
funcPtrStruct funPtrSt;
//func = &testFunc; //Here the error is shown " a value of type .....can not convert......"
funPtrSt.func = &testFuncOne; //This is working but it is not a class memeber.
}
private:
friend struct funcPtrStruct; //Make the structure of function pointers as your friend.
};
int main() {
return 0;
}https://stackoverflow.com/questions/39795049
复制相似问题