【发布时间】:2021-03-09 19:11:49
【问题描述】:
我的 Compressor 类有字段 QMap<QString, std::function<DataList(Compressor&, DataList &)> &> *techniques; 和静态方法 static DataList & sndStep(DataList &l);。它们的类型定义:
typedef QPair<QPointF, QString> Data;
typedef QList<Data> DataList;
typedef QList<DataList> DataTable;
我正在尝试创建一个QMap,它将包含技术的QString 名称作为键和技术作为值。
一开始我尝试插入对("R0", sndStep){
Compressor::Compressor(const QString& s)
{
std::function<DataList(Compressor&, Datalist&)> f =
[] (DataList & l) {
return &Compressor::sndStep(DataList & l);
};
techniques->insert("R0", f);
}
但是我有很多错误。其中之一是“没有可行的转换”:
compressor.cpp:8:53: error: no viable conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<DataList (Compressor &, DataList &)>' (aka 'function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)>')
std_function.h:402:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
std_function.h:413:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'const std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &' for 1st argument
std_function.h:422:7: note: candidate constructor not viable: no known conversion from '(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9)' to 'std::function<QList<QPair<QPointF, QString>> (Compressor &, QList<QPair<QPointF, QString>> &)> &&' for 1st argument
std_function.h:446:2: note: candidate template ignored: substitution failure [with _Functor = (lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9), $1 = void]: no type named 'type' in 'std::result_of<(lambda at C:\Qt\Projects\ChartWithMenu\compressor.cpp:9:9) &(Compressor &, QList<QPair<QPointF, QString>> &)>'
compressor.cpp:9:9: note: candidate function
如何解决此错误?或者也许有其他方法来创建函数映射(不一定是std::function)?
【问题讨论】:
-
DataList(Compressor&, Datalist&)和 lambda 签名似乎不匹配。而且&Compressor::sndStep(DataList & l);看起来不像是合法代码。 -
为什么要存储对
std::function的引用?f是一个局部变量,只要Compressor::Compressor返回,您就会有一个悬空引用。您应该按值存储,例如QMap<QString, std::function<DataList(Compressor&, DataList &)>>(没有最后一个&) -
您的代码不完整;特别是,它似乎缺少一个
main()函数和至少一个#include。请edit您的代码,这是您问题的minimal reproducible example(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试重现并解决它。您还应该阅读How to Ask。
标签: c++ qt std-function