I'm trying to create an object that both has Qt events, AND has a function that can accept a variable number of arguments. This is a minimal example, although lacking in the events, but with the QObject stuff needed for the events. The problem is that it generates a link error. It's based on the first answer to this SO ?: [Variable number of arguments in C++?][1]
#include <iostream>
#include <string>
#include <initializer_list>
#include <qobject.h>
#include <qdebug.h>
class aClass : public QObject
{
Q_OBJECT
public:
template <typename T>
void func(T t)
{
qDebug() << t;
}
template<typename T, typename... Args>
void func(T t, Args... args) // recursive variadic function
{
qDebug() << t;
func(args...) ;
}
};
int main()
{
QString str1( "Hello" );
aClass a;
a.func(1, 2.5, 'a', str1);
}
The errors I get are:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl aClass::metaObject(void)const " (?metaObject@aClass@@UEBAPEBUQMetaObject@@XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl aClass::qt_metacast(char const *)" (?qt_metacast@aClass@@UEAAPEAXPEBD@Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl aClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@aClass@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
If I remove the QObject stuff, it works. If the functions are outside the class it works. Is there a way to have both in the same class? I want to have three arguments of predetermined types as the first arguments, not of the same types, and then from zero to any number of QString arguments after that. This is for a project in c++11, but a c++17 answer would be acceptable, too. [1]: https://stackoverflow.com/a/16338804/1378243
Aucun commentaire:
Enregistrer un commentaire