【问题标题】:Why does my std::function have no viable conversion?为什么我的 std::function 没有可行的转换?
【发布时间】: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&amp;, Datalist&amp;) 和 lambda 签名似乎不匹配。而且&amp;Compressor::sndStep(DataList &amp; l); 看起来不像是合法代码。
  • 为什么要存储对std::function 的引用? f 是一个局部变量,只要 Compressor::Compressor 返回,您就会有一个悬空引用。您应该按值存储,例如QMap&lt;QString, std::function&lt;DataList(Compressor&amp;, DataList &amp;)&gt;&gt;(没有最后一个&amp;
  • 您的代码不完整;特别是,它似乎缺少一个main() 函数和至少一个#include。请edit您的代码,这是您问题的minimal reproducible example(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试重现并解决它。您还应该阅读How to Ask

标签: c++ qt std-function


【解决方案1】:

在这里,您将把 f 定义为一个接受两个参数 (Compressor&amp;, Datalist&amp;) 并返回 DataList 的函数:

std::function<DataList(Compressor&, Datalist&)> f

但这不是 lambda 的签名:

    [] (DataList & l) {
        return &Compressor::sndStep(DataList & l);
    };

它只接受一个参数 (DataList&amp;) 并返回您所显示的代码 sn-p 中不知道的东西。

【讨论】:

  • lambda 无法编译...对吧?
  • @AyxanHaqverdili 不,反正我看不到。也许有一些奇异的宏帮助:-)
猜你喜欢
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多