這是我第一次嘗試使用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