3
votes

I'm trying to use the TeamSpeak SDK for a personal project in Qt, when I use this code in the main it works fine

It compiles without problem. The problem is when I use it in Qt Mainwindow:

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_H

Mainwindow.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 */
}
}

I've commented on the line I got the error in Mainwindow.cpp and the error:

cannot convert 'MainWindow::onClientConnected' from type 'void (MainWindow::)(uint64, anyID, uint64, unsigned int*) {aka void (MainWindow::)(long long unsigned int, short unsigned int, long long unsigned int, unsigned int*)}' to type 'void ()(uint64, anyID, uint64, unsigned int) {aka void ()(long long unsigned int, short unsigned int, long long unsigned int, unsigned int)}' funcs.onClientConnected = onClientConnected; ^

I am using Windows 10 Mingw compiler Qt 5.6.1
how can i use this call back fucntion in oop c++

2

2 Answers

2
votes

I solve my problem to use TeamSpeak in Qt I initialize the server in the main.cpp and assign all call back functions in the struct and now I can use any function of the server in the main window for example if I want to show the channels in a text edit i use the function of it in any c++ class or Qt Dialog and I can call it without problems the code of the 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
}

use all function to edit the server in any c++ class it will work and i think we don't need to initialize the server more than once in main and we don't need to but it in class and if I want to make a VoIP using Qt GUI we need only the server edit function if there's a better answer please post it Thanks Edit
another solution is to initialize the struct with the call back functions in main and pass it in Mainwindow or any c++ class in the constructor and use it to initialize server lib of TeamSpeak

0
votes

Your issue can be simulated with below simple code. Based on my understanding the function you are trying to assign is a class specific member. So the compiler considers it as a signature mismatch when you are trying to assign. You may need to write the required functionality in a non class member and then assign. (unless there is a way to allow this assignment).

Make the structure with function inputs as a friend of your functional class as shown below.

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