我在 MSVS 2010 下编译某些代码时遇到问题,并且出现 C2678 错误。我在这里找到了该错误的描述:
http://msdn.microsoft.com/en-us/library/ys0bw32s(v=vs.100).aspx
但我不明白“固定本地成员”是什么意思。
有人可以解释吗?
以下是生成该错误的部分代码:
#pragma once
#include <functional>
#include <vector>
#include <memory.h>
template<typename callbackCodes_e, callbackCodes_e endOfCodes, typename param_t>
class CCallbackContainer
{
typedef std::function<void(param_t)> callback_t;
public:
struct callbackInfo_t
{
callbackCodes_e code;
callback_t callback;
param_t param;
size_t countdownToAutoDelete;
};
CCallbackContainer() {};
~CCallbackContainer() {};
typedef std::function<void(param_t)> callback_t;
typedef callbackInfo_t HANDLE;
std::vector<callbackInfo_t> m_Callbacks[endOfCodes];
void callbackCall(callbackInfo_t const & callbackInfo);
public:
inline bool validCode(callbackCodes_e code) { return code < endOfCodes; }
HANDLE callbackSet(callbackCodes_e code, callback_t callback, DWORD param, size_t countdownToAutoDelete = 1);
void callbackClear(HANDLE callbackToRemove);
void callbackCall(callbackCodes_e code)
{
auto callbackList = m_Callbacks[code];
for(auto i: callbackList)
{
i->callback(i->param);
}
for_each(auto i = callbackList.rbegin(); i != callbackList.rend(); ++i)
{
if (--(i->countdownToAutoDelete) < 1)
{
callbackList.erase(i);
}
}
}
};
// template implimentation has to be in header file
template<typename callbackCodes_e, callbackCodes_e endOfCodes, typename param_t>
typename CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::HANDLE // return
CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::callbackSet // function name
(callbackCodes_e code, callback_t callback, DWORD param, size_t countdownToAutoDelete) // parameters
{
ASSERT(validCode(code));
callbackInfo_t callbackInfo = { code, callback, param, countdownToAutoDelete };
ASSERT(std::find(m_Callbacks[code].begin(), m_Callbacks[code].end(), callback) == m_Callbacks[code].end());
m_Callbacks[code].push_back(callbackInfo);
return callbackInfo;
}
template<typename callbackCodes_e, callbackCodes_e endOfCodes, typename param_t>
void // return type
CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::callbackClear // function name
(typename CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::HANDLE callbackToRemove) // parameters
{
callbackCodes_e code = callbackToRemove.code;
ASSERT(validCode(code));
auto iCallbackInfo = std::find(m_Callbacks[code].begin(), m_Callbacks[code].end(), callbackToRemove);
ASSERT(iCallbackInfo == m_Callbacks[code].end());
m_Callbacks[code].erase(iCallbackInfo);
}
template<typename callbackCodes_e, callbackCodes_e endOfCodes, typename param_t>
void // return type
CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::callbackCall // function name
(callbackInfo_t const & callbackInfo) // parameters
{
callbackInfo.callback(callbackInfo.param);
if (--callbackInfo.countdownToAutoDelete < 1)
{
callbackClear(callbackInfo);
}
}
template<typename callbackCodes_e, callbackCodes_e endOfCodes, typename param_t>
void // return type
CCallbackContainer<callbackCodes_e, endOfCodes, param_t>::callbackCall // function name
(callbackCodes_e code) // parameters
{
auto callbackList = m_Callbacks[code];
for(auto i: callbackList)
{
i->callback(i->param);
}
for_each(auto i = callbackList.rbegin(); i != callbackList.rend(); ++i)
{
if (--(i->countdownToAutoDelete) < 1)
{
callbackList.erase(i);
}
}
}
然而,该错误实际上是由算法中的某些东西产生的:
14>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(41): error C2678: binary '==' : no operator found which take a left-hand operand of type 'CCallbackContainer:: callbackInfo_t'(或没有可接受的转换) 14> 与 14> [ 14> callbackCodes_t=CballoonHelp::callbackCodes_t, 14> endOfCodes=eCallbackEnd, 14> 参数_t=DWORD 14>] 14> 可以是“内置 C++ 运算符==(std::_Bool_type, std::_Bool_type)” 14> c:\program files (x86)\microsoft visual studio 10.0\vc\include\functional(277): 或 'bool std::tr1::operator ==(std::tr1::_Unutterable,const std:: tr1::function &)' [使用参数相关查找找到] 14> 与 14> [ 14> 参数_t=DWORD, 14> _Fty=空(双字) 14>] 14> c:\projects\cv-7646\og50\include\gxcoll.h(67): 或 'BOOL operator ==(const GXNDX &,const GXNDX &)' [使用参数相关查找找到] 14> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\guiddef.h(192): or 'int operator ==(const GUID &,const GUID &)' [使用参数找到-依赖查找] // ... 与签名不匹配的其他 operator==() 测试。
在我看来,callbackInfo_t 的默认 operator==() 存在问题,但我不确定为什么。