解决Visual Studio 2005 SP1编译QT代码的BUG
产品:QT 4.1.4 & QT 4.2.2
平台:Visual Studio 2005 Service Pack 1 (SP1)
错误:error C2244: unable to match function definition to an existing declaration
现象:
在qmap.h头文件中出现:error C2244: 'QMultiMap
(据说在qhash.h中也会出现)
参考URL:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=214100
原因:编译器升级后,分不清嵌套类型的名字是指哪一个,哪怕它们很明显地是同一个类型。例如,如下代码就会引发此错误:
template
class Foo
{
public:
typedef int NestedType;
};
template
class Bar : public Foo
{
public:
typename Foo
};
template
typename Foo
{
return 0;
}
微软的解释:
The problem is due to the use of Foo
解决方法:
在子类型中再次定义嵌套类型的名字
例如,对于QMultiMap,就是这样子
template
class QMultiMap : public QMap
{
public:
/////////////////////////////////////////////////////////////////////////
//Modified by Kenny
//Purpose: fixed the compiling-time error of Visual Studio 2005 SP1
//Ref:
typedef typename QMap
//End of modification
/////////////////////////////////////////////////////////////////////////
QMultiMap() {}
QMultiMap(const QMap
inline QMapIteratorType replace(const Key &key, const T &value);
inline QMapIteratorType insert(const Key &key, const T &value);
inline QMultiMap &operator+=(const QMultiMap &other)
{ unite(other); return *this; }
inline QMultiMap operator+(const QMultiMap &other) const
{ QMultiMap result = *this; result += other; return result; }
private:
T &operator[](const Key &key);
const T operator[](const Key &key) const;
};
/////////////////////////////////////////////////////////////////////////////
//Modified by Kenny
//Purpose: fixed the compiling-time error of Visual Studio 2005 SP1
//Ref: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=214100
template
Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap
{ return QMap
template
Q_INLINE_TEMPLATE Q_TYPENAME QMultiMap
{ return QMap
//The old code is commented out as the following lines
//template
//Q_INLINE_TEMPLATE Q_TYPENAME QMap
//{ return QMap
//
//template
//Q_INLINE_TEMPLATE Q_TYPENAME QMap
//{ return QMap
//End of modification
/////////////////////////////////////////////////////////////////////////////
没有评论:
发表评论