指向与对象类型不兼容的成员类型的指针→原因是什么? [英] Pointer to member type incompatible with object type → What is the cause?

查看:133
本文介绍了指向与对象类型不兼容的成员类型的指针→原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了这样的编译器(GNU g ++ 4.9.2)错误:

Recently I ran into a compiler (GNU g++ 4.9.2) error like this:

ProceduralTimerTaskAdapter.cpp:25:13: error: pointer to member type ‘void (Poco::Util::Timer::)(Poco::Util::TimerTask&)’ incompatible with object type ‘Poco::Util::ProceduralTimerTaskAdapter’

这是相关的代码(除了必要的Poco库外,它几乎是独立的):

Here is the relevant code (which is almost self-contained, save for the necessary Poco libs):

ProceduralTimerTaskAdapter.h:

ProceduralTimerTaskAdapter.h:

#include <Poco/Util/Timer.h>
#include <Poco/Util/TimerTask.h>
#include <Poco/Util/TimerTaskAdapter.h>

#ifndef PROCEDURALTIMERTASKADAPTER_H
#define PROCEDURALTIMERTASKADAPTER_H

using namespace std;
using namespace Poco::Util;

typedef void (*Callback) (TimerTask&);

namespace Poco {
  namespace Util {
    class ProceduralTimerTaskAdapter : public TimerTaskAdapter <Timer> {
    public:
      ProceduralTimerTaskAdapter (Callback procedure); // Constructor

      void run (); // Method defining the main thread
    protected:
      ~ProceduralTimerTaskAdapter (); // Destructor (not for general use)
    private:
      ProceduralTimerTaskAdapter (); // Default constructor (not for general use)

      Callback procedure; // The callback procedure called by the timer.
    };
  }
}

#endif

ProceduralTimerTaskAdapter.cpp:

ProceduralTimerTaskAdapter.cpp:

// This is the implementation of the ProceduralTimerTaskAdapter class.

#include <iostream>
#include <Poco/Util/Timer.h>
#include <Poco/Util/TimerTask.h>
#include <Poco/Util/TimerTaskAdapter.h>
#include "ProceduralTimerTaskAdapter.h"

using namespace std;
using namespace Poco::Util;

ProceduralTimerTaskAdapter::ProceduralTimerTaskAdapter (Callback procedure) : TimerTaskAdapter<Timer>::TimerTaskAdapter (*(new Timer ()), procedure)
{
  this -> procedure = procedure;
}

ProceduralTimerTaskAdapter::~ProceduralTimerTaskAdapter ()
{
}

void ProceduralTimerTaskAdapter::run ()
{
  TimerTask &task = *this;

  (this ->* procedure) (task);
}

事实上,我想做的是构建众所周知的TimerTaskAdapter的扩展以处理回调函数,这些回调函数绑定到特定类(因为它们位于main.cpp).我用一个非常简单的自制方法覆盖了虚拟方法run (),该方法调用了回调.处理了几种不同的错误后,我最终遇到了这种明显的类不匹配问题,无法解决自己的问题.我什至不明白为什么编译器会声明一个类名,其名称为Poco::Util::Timer::(为什么以::结尾?).当ProceduralTimerTaskAdapter定义一个名为procedure的成员时,为什么编译器需要另一个类?

What I wanna do is, in fact, build an extension of the well-known TimerTaskAdapter to handle callback functions, which are not tied to a specific class (because they are situated in main.cpp, for instance). I override the virtual method run () with a very simple self-made one, which calls the callback. After having handled several different errors, I ended up with this apparent class mismatch I can't solve myself. I even don't understand why the compiler states a class name, whose name is Poco::Util::Timer:: (Why does it end with ::?). As ProceduralTimerTaskAdapter defines a member named procedure, why does the compiler expect another class?

谢谢.

推荐答案

Poco::Util::TimerTask派生(与Poco::Util::TimerTaskAdapter类类似),并覆盖将在其中调用过程的run方法.

Derive from Poco::Util::TimerTask (like in Poco::Util::TimerTaskAdapter class) and override run method in which you will call procedures.

class ProcedureAdapter : public Poco::Util::TimerTask {
public:

    typedef void (*Callback)(TimerTask&);

    ProcedureAdapter (Callback c) : callback(c) {;}

    void run () {
        callback(*this); // call some procedure which takes TimerTask
    }

    Callback callback;
};

void fun (Poco::Util::TimerTask&) {
    cout << "fun was invoked" << endl;
}

void fun2 (Poco::Util::TimerTask&) {
    cout << "fun2 was invoked" << endl; 
}

int main()
{
    Poco::Util::Timer t;
    t.schedule (new ProcedureAdapter{&fun},1,1);
    t.schedule (new ProcedureAdapter{&fun2},1,1);

这篇关于指向与对象类型不兼容的成员类型的指针→原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆