这是我第一次尝试使用QtConcurrent :: blockingMappedReduced,并且我无法在MSVC 2010 Express(使用QT 4.7.1源代码)中构建它。QtConcurrent blockingMappedReduced error
我创建了一个小例子,类似于我的实际代码,并有同样的问题建筑:
// Here's the general outline:
// 1. create a list of random numbers
// 2. pass the list to blockingMappedReduced
// 3. the map function calculates the sine of the given random number
// 4. the reduce function finds the random number with the maximum sine value
// Here's the implementation:
#include "stdafx.h"
#include <qlist.h>
#include <qtconcurrentmap.h>
// My class for the map/reduce functions
class myClass
{
private:
// Nested class to hold the intermediate results from the map function
// I think I need this because the reduce function needs more from the map function than a single return value
class Temp
{
public:
// For example, let's pass these two member variables from the map function to the reduce function
int randomInput;
double resultingOutput;
// The Temp constructor
Temp::Temp(double randomInput, double resultingOutput)
{
this->randomInput = randomInput;
this->resultingOutput = resultingOutput;
}
};
public:
// For example, these myClass members will hold the final result from the reduce function
double maximumOutput;
double maximumInput;
// The myClass constructor
myClass::myClass()
{
this->maximumOutput = -1;
}
// The map function
const Temp mapFunction(const double& randomInput)
{
// For example, let's calculate the sine of the given random number
double resultingOutput = sin(randomInput);
// Construct the temporary structure to pass multiple values to the reduce function
const Temp temp(randomInput, resultingOutput);
return(temp);
}
// The reduce function
void reduceFunction(double& maxInput, const Temp& temp)
{
// For example, let's find the maximum computed sine value
if (temp.resultingOutput > this->maximumOutput)
{
this->maximumOutput = temp.resultingOutput;
this->maximumInput = temp.randomInput;
}
maxInput = this->maximumInput;
}
};
// Main function
void main(int argc, _TCHAR* argv[])
{
// Build a list of random numbers
QList<int> aList;
for (int count = 8; count > 0; --count)
{
aList.append(rand());
}
// Invoke the parallel map/reduce function
myClass myClassInstance;
double theAnswer = QtConcurrent::blockingMappedReduced(aList, &myClass::mapFunction, &myClass::reduceFunction);
}
编译器抱怨的最后一行,在那里blockingMappedReduced被调用。
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'D QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__thiscall C::*)(void) const,U (__thiscall D::*)(V),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(659) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'C QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__cdecl *)(U),V (__thiscall C::*)(W),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(643) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'V QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__thiscall C::*)(void) const,U (__cdecl *)(V &,W),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(627) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'W QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__cdecl *)(U),V (__cdecl *)(W &,X),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(611) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'ResultType QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__thiscall C::*)(void) const,ReduceFunctor,QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(595) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'ResultType QtConcurrent::blockingMappedReduced(Iterator,Iterator,T (__cdecl *)(U),ReduceFunctor,QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(579) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'C QtConcurrent::blockingMappedReduced(Iterator,Iterator,MapFunctor,T (__thiscall C::*)(U),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(563) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'U QtConcurrent::blockingMappedReduced(Iterator,Iterator,MapFunctor,T (__cdecl *)(U &,V),QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(547) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2780: 'ResultType QtConcurrent::blockingMappedReduced(Iterator,Iterator,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : expects 5 arguments - 3 provided
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(536) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'D QtConcurrent::blockingMappedReduced(const Sequence &,T (__thiscall C::*)(void) const,U (__thiscall D::*)(V),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__thiscall C::*)(void) const' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(522) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'C QtConcurrent::blockingMappedReduced(const Sequence &,T (__cdecl *)(U),V (__thiscall C::*)(W),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__cdecl *)(U)' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(508) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'V QtConcurrent::blockingMappedReduced(const Sequence &,T (__thiscall C::*)(void) const,U (__cdecl *)(V &,W),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__thiscall C::*)(void) const' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(494) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'W QtConcurrent::blockingMappedReduced(const Sequence &,T (__cdecl *)(U),V (__cdecl *)(W &,X),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__cdecl *)(U)' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(480) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'ResultType QtConcurrent::blockingMappedReduced(const Sequence &,T (__thiscall C::*)(void) const,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__thiscall C::*)(void) const' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(466) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'ResultType QtConcurrent::blockingMappedReduced(const Sequence &,T (__cdecl *)(U),ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__cdecl *)(U)' from 'const myClass::Temp (__thiscall myClass::*)(const double &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(452) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'C QtConcurrent::blockingMappedReduced(const Sequence &,MapFunctor,T (__thiscall C::*)(U),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__thiscall C::*)(U)' from 'void (__thiscall myClass::*)(double &,const myClass::Temp &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(438) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2784: 'U QtConcurrent::blockingMappedReduced(const Sequence &,MapFunctor,T (__cdecl *)(U &,V),QtConcurrent::ReduceOptions)' : could not deduce template argument for 'T (__cdecl *)(U &,V)' from 'void (__thiscall myClass::*)(double &,const myClass::Temp &)'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(424) : see declaration of 'QtConcurrent::blockingMappedReduced'
c:\gwa\tmp\stackoverflow\stackoverflow\stackoverflow.cpp(77): error C2783: 'ResultType QtConcurrent::blockingMappedReduced(const Sequence &,MapFunctor,ReduceFunctor,QtConcurrent::ReduceOptions)' : could not deduce template argument for 'ResultType'
c:\gwa\third_party\qt\qt-4.7.1\src\corelib\concurrent\qtconcurrentmap.h(414) : see declaration of 'QtConcurrent::blockingMappedReduced'
从网上QT文档(http://doc.qt.nokia.com/4.7-snapshot/qtconcurrentmap.html#blockingMappedReduced),这里的原型:
T blockingMappedReduced (const Sequence & sequence, MapFunction mapFunction, ReduceFunction reduceFunction, QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce)
我害怕我没有专业知识去调试。任何帮助将不胜感激。再一次,因为我的C++技能不是专家,所以帮助需要明确以便我理解它(例如,实际的代码片段,而不是那些比我有更多知识的东西,比如“你的论点必须是一个常量引用” )。
在此先感谢您的帮助。
HostileFork:感谢您的解决方案。除了一般的C++清理以外,下面是与blockingMappedReduced相关的问题:1)map和reduce函数不能成为成员函数,但必须是全局函数,2)我的中间类从地图传递值函数的reduce函数需要一个默认的构造函数,3)map函数的类型不是const。后续问题:QT文档给出了一个使用map和reduce为QtConcurrent :: mappedReduced使用成员函数的示例。但是这不适用于blockingMappedReduced? – user1020872
要回答我自己的后续问题,使用成员函数时,显然它们必须是静态的。 – user1020872
我可以问你一些关于你的C++清理我的代码的问题吗? 1)为什么中间对象使用结构而不是类是可取的? 2)为什么最好没有中间对象的嵌套类?我的动机是,我的实际代码中的myClass是封装了许多其他事物的类,而不仅仅是我在示例中展示的简单事物。 – user1020872