简体   繁体   English

成员函数调用在 Qt 函数“run”中不起作用。 我如何在“运行”中正确使用我的模板?

[英]member function call doesn´t work in Qt function "run". How I use my template in "run" correctly?

This is my template这是我的模板

template <class T>
void writeData(QVector<T> &vec, const QString &fp, void(T::*method_ptr)(QFile&) )
{
    QFile f(fp);
    if(!f.open(QIODevice::WriteOnly | QIODevice::Append) )
    {
        qDebug() << "File error" << f.error();
    }
    else
    {
        QThread::currentThread();
        for(T &tw : vec)
        {
            (tw.*method_ptr)(f);
        }
    }
    f.close();
}

And here I would use my template:在这里,我将使用我的模板:

//teams is like: QVector<teamType> teams
QFuture<void> fut = run(writeData, teams, filePath, &teamType::writeTeams); // no matching function for call 'run'
                    ~~~                              ~~~~~~~~~~~~~~~~~~~
//So, what´s wrong here?

Because this works fine:因为这工作正常:

writeData(teams, filePath, &teamType::writeTeams);

And "&teamType::writeTeams" cames from the following:而 "&teamType::writeTeams" 来自以下内容:

void teamType::writeTeams(QFile &f)
{
    QTextStream out(&f);
    out.setCodec("UTF-16LE");
    out << teamId << "\t" << offsideTrap << "\t" << withoutBall << "\t" << formationId << "\t"
            << attack << "\t" << teamMentality << "\t" << attackTactic1 << "\t"
            << attackTactic2 << "\t" << defenseTactic1 << "\t" << defenseTactic2 << "\t" << captain << "\t"
            << penaltyTakerId << "\t" << kickTakerId << "\t" << leftCornerkickTakerId << "\t" << rightCornerkickTakerId << "\t"
            << numTransfersIn << endl;
}

It´sa member function in class "teamType"它是“teamType”类中的成员函数

because writeData is a function template there is no mechanism that run can use to get the correct instantiation of that template for the parameters.因为writeData是一个函数模板,所以run没有任何机制可以用来为参数获取该模板的正确实例化。

A real easy fix for this is to just wrap the call in a lambda.一个真正简单的解决方法是将调用包装在 lambda 中。 That would look like那看起来像

QFuture<void> fut = run([=](){ writeData(teams, filePath, &teamType::writeTeams); });

and now that the call to writeData is actually done with the function parameters, template argument deduction will succeed and the code will compile.现在对writeData的调用实际上是使用函数参数完成的,模板参数推导将成功,代码将编译。

I got the solution, Everything works fine now:我得到了解决方案,现在一切正常:

template <class T>
void writeTeamsForLoop(QVector<T> &vec, const QString fp, void(T::*method_ptr)(QFile&) ) 
{                                       //QString must not be a reference(const QString fp)!
    QFile f(fp);
    if(!f.open(QIODevice::WriteOnly | QIODevice::Append) )
    {
        qDebug() << "File error" << f.error();
    }
    else
    {
        QThread::currentThread();
        for(T &tw : vec)
        {
            (tw.*method_ptr)(f);
        }
    }
    f.close();
}

Lambda code:拉姆达代码:

QFuture<void> fut = run([&, filePath]{ writeData(teams, filePath, &teamType::writeTeams); } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM