成员函数调用在 Qt 函数中不起作用 "run"。我如何正确使用 "run" 中的模板?
member function call doesn´t work in Qt function "run". How I use my template in "run" correctly?
这是我的模板
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();
}
这里我会使用我的模板:
//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?
因为这很好用:
writeData(teams, filePath, &teamType::writeTeams);
而“&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;
}
它是 class "teamType"
中的一个成员函数
因为 writeData
是一个函数模板,所以 run
没有机制可以用来为参数获取该模板的正确实例化。
一个真正简单的解决方法是将调用包装在 lambda 中。看起来像
QFuture<void> fut = run([=](){ writeData(teams, filePath, &teamType::writeTeams); });
现在对 writeData
的调用实际上是用函数参数完成的,模板参数推导将成功,代码将编译。
我得到了解决方案,现在一切正常:
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 代码:
QFuture<void> fut = run([&, filePath]{ writeData(teams, filePath, &teamType::writeTeams); } );
这是我的模板
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();
}
这里我会使用我的模板:
//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?
因为这很好用:
writeData(teams, filePath, &teamType::writeTeams);
而“&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;
}
它是 class "teamType"
中的一个成员函数因为 writeData
是一个函数模板,所以 run
没有机制可以用来为参数获取该模板的正确实例化。
一个真正简单的解决方法是将调用包装在 lambda 中。看起来像
QFuture<void> fut = run([=](){ writeData(teams, filePath, &teamType::writeTeams); });
现在对 writeData
的调用实际上是用函数参数完成的,模板参数推导将成功,代码将编译。
我得到了解决方案,现在一切正常:
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 代码:
QFuture<void> fut = run([&, filePath]{ writeData(teams, filePath, &teamType::writeTeams); } );