0

我正在尝试在对象之间创建一个通信系统,其中控制器对象保留其他对象的成员函数向量,并在需要时调用/调用它们。
我阅读了几篇关于使用的文章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 文件xmemory0vector. 错误是:

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。)

4

2 回答 2

2

查看您的代码,看起来您最好先存储std::function<double(double, int)>,然后使用std::bind将对象绑定到成员函数。

template <typename F>
void AddSubscriber( const Callable & obj,
                    F subscriber)
{
    Subscribers.push_back(std::bind(subscriber, obj, _1, _2));
}

或者只是通过直接传递一些可转换的东西来完全Callable解耦:Callerstd::function<double(double, int)>

class Caller 
{
public:
  template <typename F>
  void AddSubscriber(F&& subscriber)
  {
     subscribers.emplace_back(std::forward<F>(subscriber));
  }
  void CallSubscribers()
  {
      for (const auto& subscriber : subscribers)
      {
          subscriber(1.0, 2);
      }
  }
 private:
  std::vector<std::function<double(double, int)> subscribers;
};

然后从调用方进行绑定。

Caller c;
ClassA a;
using namespace std::placeholders;
c.AddSubscriber(std::bind(&ClassA::CallThis, a, _1, _2));
于 2013-07-31T09:53:11.020 回答
0

阅读 juanchopanza 的答案后,我重写了我的代码,如下所示。现在它按预期工作。

class ClassA
{
    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:
        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(std::function<double(double, int)> Subscriber)
        {
            Subscribers.push_back(Subscriber);
        }
        void CallSubscribers()
        {
            for (const auto & Subscriber : Subscribers)
            {
                Subscriber(1.0, 2);
            }
        }
    private:
        std::vector<std::function<double(double, int)>> Subscribers;
};

int APIENTRY wWinMain(  _In_        HINSTANCE   hInstance,
                        _In_opt_    HINSTANCE   hPrevInstance,
                        _In_        LPTSTR      lpCmdLine,
                        _In_        int         nCmdShow)
{
    // ...

    ClassA ObjectA;
    ClassB ObjectB;
    Caller ObjectCaller;
    ObjectCaller.AddSubscriber(std::bind(&ClassA::CallThis, ObjectA, std::placeholders::_1, std::placeholders::_2));
    ObjectCaller.AddSubscriber(std::bind(&ClassB::CallMe,   ObjectB, std::placeholders::_1, std::placeholders::_2));
    Sleep(5000);
    ObjectCaller.CallSubscribers();

    // ...
}
于 2013-07-31T12:47:16.310 回答