我正在尝试在对象之间创建一个通信系统,其中控制器对象保留其他对象的成员函数向量,并在需要时调用/调用它们。
我阅读了几篇关于使用的文章std::function
,但我找不到涵盖此用例的文章。
我的代码是:
#include <functional>
#include <Windows.h>
class Callable
{
};
class ClassA : public Callable
{
public:
double CallThis(double x, int y) const
{
MessageBoxW(NULL, L"CallThis() is called.", L"In ClassA", MB_ICONINFORMATION);
return x + y;
}
};
class ClassB : public Callable
{
public:
double CallMe(double x, int y) const
{
MessageBoxW(NULL, L"CallMe() is called.", L"In ClassB", MB_ICONINFORMATION);
return x + y;
}
};
class Caller
{
public:
void AddSubscriber( const Callable & ObjectToCall,
const std::function<double(const Callable &, double, int)> & Subscriber)
{
Subscribers.push_back(Subscriber);
ObjectsToBeCalled.push_back(ObjectToCall);
}
void CallSubscribers()
{
for (size_t i=0; i<Subscribers.size(); i++)
{
Subscribers[i](ObjectsToBeCalled[i], 1.0, 2);
}
}
private:
std::vector<std::function<double(const Callable &, double, int)>> Subscribers;
std::vector<const Callable &> ObjectsToBeCalled;
};
int APIENTRY wWinMain( _In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
// ...
ClassA ObjectA;
ClassB ObjectB;
Caller ObjectCaller;
ObjectCaller.AddSubscriber(ObjectA, &ClassA::CallThis);
ObjectCaller.AddSubscriber(ObjectA, &ClassB::CallMe);
Sleep(5000);
ObjectCaller.CallSubscribers();
// ...
}
奇怪的是,当我运行此代码时,我自己的代码中没有任何编译器错误,但我从 STL 文件xmemory0
和vector
. 错误是:
Error 1 xmemory0 527 error C2528: 'pointer' : pointer to reference is illegal
Error 2 xmemory0 528 error C2528: 'const_pointer' : pointer to reference is illegal
Error 3 xmemory0 561 error C2535: 'const Callable &(*std::allocator<_Ty>::address(const Callable &) throw() const)' : member function already defined or declared
Error 4 xmemory0 599 error C2528: '_Ptr' : pointer to reference is illegal
Error 5 xmemory0 604 error C2528: '_Ptr' : pointer to reference is illegal
Error 6 xmemory0 700 error C2528: 'pointer' : pointer to reference is illegal
Error 7 xmemory0 701 error C2528: 'const_pointer' : pointer to reference is illegal
Error 8 xmemory0 824 error C2535: 'const Callable &(*std::_Wrap_alloc<_Alloc>::address(const Callable &) const)' : member function already defined or declared
Error 9 xmemory0 890 error C2528: '_Ptr' : pointer to reference is illegal
Error 10 xmemory0 105 error C2528: 'abstract declarator' : pointer to reference is illegal
Error 11 xmemory0 107 error C2528: 'abstract declarator' : pointer to reference is illegal
Error 12 xmemory0 122 error C2528: 'pointer' : pointer to reference is illegal
Error 13 xmemory0 123 error C2528: 'const_pointer' : pointer to reference is illegal
Error 14 vector 773 error C2528: '_Pval' : pointer to reference is illegal
Error 15 vector 1184 error C2535: 'void std::vector<_Ty>::push_back(const Callable &)' : member function already defined or declared
Error 16 vector 1245 error C2535: 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::insert(std::_Vector_const_iterator<_Myvec>,_Ty)' : member function already defined or declared
Error 17 vector 1494 error C2528: '_Ptr' : pointer to reference is illegal
Error 18 vector 1658 error C2528: '_Pval' : pointer to reference is illegal
我究竟做错了什么?如何使此代码运行?
(我的 IDE 是 Microsoft Visual Studio 2012。)