【问题标题】:What´s wrong with this template in Qt? QFile deleted constructor?Qt 中的这个模板有什么问题? QFile 删除了构造函数?
【发布时间】:2020-06-28 14:27:01
【问题描述】:

所以,这是我的模板函数。我得到错误消息“调用'QFile'的已删除构造函数”。 我怎样才能解决这个问题?或者你能告诉我推荐,我怎样才能以最好的方式把这个函数写成模板?

template <class T>     // T shall be the name of a class
void example(QVector<T> &vec, const QString &fp, std::function<void(QFile)> func) //func is a method from a class
{
    QFile f(fp);
    if(!f.open(QIODevice::WriteOnly | QIODevice::Append) )
    {
        qDebug() << "File error" << f.error();
    }
    else
    {
        QThread::currentThread();
        for(T &tw : vec)
        {
            //tw.func(f);     
            func(f).tw;      **//call to deleted constructor of 'QFile'**                   
        }
    }
    f.close();
}

func 可以是这样的,例如:

void writeTeamData(QFile&);
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;
}

【问题讨论】:

    标签: c++ qt templates methods qfile


    【解决方案1】:

    您无法复制QFile,因为复制构造函数已被删除。 将example 的签名改为std::function&lt;void(QFile&amp;)&gt; func

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      相关资源
      最近更新 更多