模板化的“监听器”处理程序歧义问题

发布于 2024-10-24 23:21:17 字数 1814 浏览 1 评论 0原文

我使用 JUCE 作为 x 平台框架,并且使用模板侦听器类将按钮/组合框等回调映射到某些处理程序函数。由于不同的小部件有自己的回调函数名称,因此我使用以下结构:

template<typename Type, typename Widget>
class ListenerBase : public Widget::Listener
{
public:
  typedef void (Type::*TCallbackType)(void);
protected:
  void notifyCallback(Widget* notifier)
  {
  ...
  }
  void addHandler(Widget* notifier, TCallbackType callback)
  {
    notifier->addListener(this);
  ...
  }
};

template<typename Type>
class ButtonListenerHandler : public ListenerBase<Type, Button>
{
protected:
  void buttonClicked(Button* btn)
  {
     notifyCallback(btn);
  }
};

template<typename Type>
class LabelListenerHandler : public ListenerBase<Type, Label>
{
protected:
  void labelTextChanged(Label* lbl)
  {
     notifyCallback(lbl);
  }
};

只要我在类中仅使用处理程序专业化之一,它就可以正常工作。一旦我使用多个,VC++ 2008 就会抱怨 addHandler 调用之间存在歧义,就好像编译器无法区分 addHandler(Button*, ...) 和 addHandler(Label*, ...) !由于模板化,这些函数具有不同的原型,所以我不知道为什么编译器给我带来困难。想法?

根据请求进行编辑:

具有不同侦听器的类可能如下所示:

class MyClass : public ButtonListenerHandler<MyClass>
              , public LabelListenerHandler<MyClass>
{
...
   void buttonHandlerFunction();
   void labelHandlerFunction();

   Button* m_btn;
   Label* m_label;
};

A 发生错误的地方:

MyClass::MyClass()
{
...
   addHandler(m_btn, &MyClass::buttonHandlerFunction);  <<< error
   addHandler(m_label, &MyClass::labelHandlerFunction);  <<< error
}

错误是:

1>MyClass.cpp(287) : error C2385: ambiguous access of 'addHandler'
1>        could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Button>'
1>        or could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Label>'

I'm using JUCE as a x-platform framework, and I'm using template listener classes to map button/combobox etc. callbacks to certain handler functions. Since the different widgets have their own callback function name, I use the following structure:

template<typename Type, typename Widget>
class ListenerBase : public Widget::Listener
{
public:
  typedef void (Type::*TCallbackType)(void);
protected:
  void notifyCallback(Widget* notifier)
  {
  ...
  }
  void addHandler(Widget* notifier, TCallbackType callback)
  {
    notifier->addListener(this);
  ...
  }
};

template<typename Type>
class ButtonListenerHandler : public ListenerBase<Type, Button>
{
protected:
  void buttonClicked(Button* btn)
  {
     notifyCallback(btn);
  }
};

template<typename Type>
class LabelListenerHandler : public ListenerBase<Type, Label>
{
protected:
  void labelTextChanged(Label* lbl)
  {
     notifyCallback(lbl);
  }
};

And it works fine, as long as I use only one of the handler specializations in my class. As soon as I use more than one, VC++ 2008 complains of ambiguity between the addHandler calls as if the compiler cannot distiguish between addHandler(Button*, ...) and addHandler(Label*, ...) !! These functions are of different prototypes due to being templatized, so I have no idea why the compiler is giving me a hard time. Ideas ?

Edit due to requests:

A class with different listeners may look like:

class MyClass : public ButtonListenerHandler<MyClass>
              , public LabelListenerHandler<MyClass>
{
...
   void buttonHandlerFunction();
   void labelHandlerFunction();

   Button* m_btn;
   Label* m_label;
};

A where the error occurs:

MyClass::MyClass()
{
...
   addHandler(m_btn, &MyClass::buttonHandlerFunction);  <<< error
   addHandler(m_label, &MyClass::labelHandlerFunction);  <<< error
}

And the error is:

1>MyClass.cpp(287) : error C2385: ambiguous access of 'addHandler'
1>        could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Button>'
1>        or could be the 'addHandler' in base 'ListenerBase<MyClass,juce::Label>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-10-31 23:21:17

编辑
好吧,重新考虑一下之后,问题出在 ListenerBaseListenerBase 各自定义了一个 addHandler 函数,由于继承的原因,即使它们具有不同的签名(一个带有 Button* 参数,另一个带有 Label* 参数),它似乎也不算重载。我发现的一个可能的修复方法是完全限定对 addHandler 的调用,有点冗长,也许不是真正想要的,但它有效(为了方便起见,我 typedef 'd 基类):

template<class Type>
class ButtonListenerHandler : public ListenerBase<Type, Button>{
public:
    typedef ListenerBase<Type, Button> ButtonListenerBase;
};

template<class Type>
class LabelListenerHandler : public ListenerBase<Type, Label>{
public:
    typedef ListenerBase<Type, Label> LabelListenerBase;
};

class MyClass : public ButtonListenerHandler<MyClass>,
                public LabelListenerHandler<MyClass>{
public:
    void buttonHandlerFunction();
    void labelHandlerFunction();

    MyClass(){
        ButtonListenerHandler<MyClass>::addHandler(m_btn, &MyClass::buttonHandlerFunction);
        LabelListenerHandler<MyClass>::addHandler(m_label, &MyClass::labelHandlerFunction);
    }

private:
    Button* m_btn;
    Label* m_label;
};

'另一个编辑
感谢对 的快速回答我的问题在这里,我可以再次编辑。其中提到的 using 方法也适用于您的问题。 :)

class MyClass : public ButtonListenerHandler<MyClass>,
    public LabelListenerHandler<MyClass>{
public:
    using ButtonListenerHandler<MyClass>::addHandler;
    using LabelListener<MyClass>::addHandler;

    void buttonHandlerFunction(){
    }
    void labelHandlerFunction(){
    }

    MyClass(){
        addHandler(m_btn, &MyClass::buttonHandlerFunction);
        addHandler(m_label, &MyClass::labelHandlerFunction);
    }

private:
    Button* m_btn;
    Label* m_label;
};

Edit
Okay, after rethinking everything, the problem lies with ListenerBase<MyClass, Button> and ListenerBase<MyClass, Label> each defining an addHandler function, which because of inheritance doesn't seem to count as overloading, even though they have different signatures (one with a Button* parameter, the other with a Label* one). One possible fix I found for this is to fully qualify the call to addHandler, a bit verbose and maybe not what was really desired, but it works (for convenience, I typedef'd the base classes):

template<class Type>
class ButtonListenerHandler : public ListenerBase<Type, Button>{
public:
    typedef ListenerBase<Type, Button> ButtonListenerBase;
};

template<class Type>
class LabelListenerHandler : public ListenerBase<Type, Label>{
public:
    typedef ListenerBase<Type, Label> LabelListenerBase;
};

class MyClass : public ButtonListenerHandler<MyClass>,
                public LabelListenerHandler<MyClass>{
public:
    void buttonHandlerFunction();
    void labelHandlerFunction();

    MyClass(){
        ButtonListenerHandler<MyClass>::addHandler(m_btn, &MyClass::buttonHandlerFunction);
        LabelListenerHandler<MyClass>::addHandler(m_label, &MyClass::labelHandlerFunction);
    }

private:
    Button* m_btn;
    Label* m_label;
};

'nother Edit
Thanks to the fast answer to my question here, I can give it another edit. The using method mentioned there also applies to your problem. :)

class MyClass : public ButtonListenerHandler<MyClass>,
    public LabelListenerHandler<MyClass>{
public:
    using ButtonListenerHandler<MyClass>::addHandler;
    using LabelListener<MyClass>::addHandler;

    void buttonHandlerFunction(){
    }
    void labelHandlerFunction(){
    }

    MyClass(){
        addHandler(m_btn, &MyClass::buttonHandlerFunction);
        addHandler(m_label, &MyClass::labelHandlerFunction);
    }

private:
    Button* m_btn;
    Label* m_label;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文