我在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 takes a left-hand operand of type 'CCallbackContainer::callbackInfo_t' (or there is no acceptable conversion) 14> with 14> [ 14> callbackCodes_t=CBalloonHelp::callbackCodes_t, 14> endOfCodes=eCallbackEnd, 14> param_t=DWORD 14> ] 14> could be 'built-in C++ operator==(std::_Bool_type, std::_Bool_type)' 14> c:\program files (x86)\microsoft visual studio 10.0\vc\include\functional(277): or 'bool std::tr1::operator ==(std::tr1::_Unutterable,const std::tr1::function &)' [found using argument-dependent lookup] 14> with 14> [ 14> param_t=DWORD, 14> _Fty=void (DWORD) 14> ] 14> c:\projects\cv-7646\og50\include\gxcoll.h(67): or 'BOOL operator ==(const GXNDX &,const GXNDX &)' [found using argument-dependent lookup] 14> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\guiddef.h(192): or 'int operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup] // ... other operator==() tests that didn't match the signature.
在我看来,callbackInfo_t的默认运算符==()存在问题,但我不确定原因。
答案 0 :(得分:3)
固定是C ++ .NET中使用的术语。当ref'ed类(用ref class
声明的那些)需要访问某些本地成员变量时,它必须 pin 它,因为托管引用可以在内存中移动,但是原生指针必须留在固定的地方。一旦你 pin 成员变量,你就会获得一个本机指针,一切正常。当对象取消固定时,它将再次移动。有关详细信息,请搜索pin_ptr<>
模板。
但是您的代码根本没有ref class
,因此固定评论只会误导您!
您的问题很简单,就是您没有向operator==()
结构提供callback_t
!
只要这样做,或者更适合你情况的任何事情:
struct callbackInfo_t
{
callbackCodes_e code;
callback_t callback;
param_t param;
size_t countdownToAutoDelete;
bool operator==(const callbackInfo_t &o) const
{
return code == o.code && callback == o.callback &&
param == o.param && countdownToAutoDelete == o.countdownToAutoDelete;
}
};
但请注意,显然,您比较的任何成员变量也必须具有可比性,否则代码将再次失败并显示类似的消息。