首页 > 解决方案 > QtConcurrent::mapped 未编译

问题描述

我正在尝试使用QtConcurrent::mapped,但我无法让它工作。它应该很简单:

class ChainInfo {
};


class Chain {
public:
   ChainInfo GetInfo() const;
};


void CalculateInfo(QList<Chain *> Chains) const {
   auto GetChainInfo = [](Chain const *pChain) {
      return pChain->GetInfo();
   };

   auto ChainInfos = QtConcurrent::mapped(Chains, GetChainInfo);
}

我收到以下编译错误:

QtConcurrent/qtconcurrentmapkernel.h(162): error C2039: 'result_type': is not a member of 'CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>'
Link.cpp(193): note: see declaration of 'CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>'
QtConcurrent/qtconcurrentmapkernel.h(213): note: see reference to class template instantiation 'QtConcurrent::MappedEachKernel<QList<Chain *>::const_iterator,Functor>' being compiled
        with
        [
            Functor=CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>
        ]
QtConcurrent/qtconcurrentmapkernel.h(237): note: see reference to class template instantiation 'QtConcurrent::SequenceHolder1<Sequence,QtConcurrent::MappedEachKernel<QList<Chain *>::const_iterator,Functor>,Functor>' being compiled
        with
        [
            Sequence=QList<Chain *>,
            Functor=CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>
        ]
qtconcurrent\qtconcurrentmap.h(132): note: see reference to function template instantiation 'QtConcurrent::ThreadEngineStarter<void> QtConcurrent::startMapped<void,Sequence,T>(const Sequence &,Functor)' being compiled
        with
        [
            Sequence=QList<Chain *>,
            T=CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>,
            Functor=CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>
        ]
Link.cpp(195): note: see reference to function template instantiation 'QFuture<void> QtConcurrent::mapped<QList<Chain *>,CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>>(const Sequence &,MapFunctor)' being compiled
        with
        [
            Sequence=QList<Chain *>,
            MapFunctor=CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>
        ]
QtConcurrent/qtconcurrentmapkernel.h(162): error C2146: syntax error: missing '>' before identifier 'result_type'
QtConcurrent/qtconcurrentmapkernel.h(165): error C2039: 'result_type': is not a member of 'CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>'
Link.cpp(193): note: see declaration of 'CalculateInfo::<lambda_7571384993bda001f6e6e0e4e3ad7d4a>'
QtConcurrent/qtconcurrentmapkernel.h(165): error C3646: 'T': unknown override specifier
QtConcurrent/qtconcurrentmapkernel.h(165): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
QtConcurrent/qtconcurrentmapkernel.h(167): error C3646: 'ReturnType': unknown override specifier
QtConcurrent/qtconcurrentmapkernel.h(167): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
QtConcurrent/qtconcurrentmapkernel.h(237): error C2440: 'return': cannot convert from 'QtConcurrent::ThreadEngineStarter<int>' to 'QtConcurrent::ThreadEngineStarter<void>'
QtConcurrent/qtconcurrentmapkernel.h(237): note: No constructor could take the source type, or constructor overload resolution was ambiguous
         dpclosedguielement.cpp
         dpclusterelement.cpp
         dpdimensioncontroller.cpp
         dpexternaldocumentsload.cpp
         dpflowsymbol.cpp
         dpguilink.cpp
         dpguisymbol.cpp

有什么想法可能是错的吗?

标签: c++qtlambdaconcurrency

解决方案


我能够通过指定 lambda 函数的类型来解决它,而不仅仅是使用auto

std::function<ChainInfo(Chain const *)> GetChainInfo = [](Chain const *pChain) {
   return pChain->GetInfo();
};

推荐阅读