2017-01-28 4 views
4

私はポインタ関数としてメソッドを渡そうとしていますので、バインダーを作成しましたが、hereと表示されますが、メソッドが定義されているので、メソッドとしてパラメータとして渡すことができません。バインダー。メソッドポインタを渡す必要がある関数は、arduinoのRegex Lua Patternライブラリからのもので、hereです。 ms.GlobalMatchポインタ関数としてtypedefメソッドを渡す

void InterpreterClass::init() 
{ 
    MatchState ms("255.255.255.255"); 

    bind_regex_member<InterpreterClass, &InterpreterClass::MatchAddressCallback, 0> b(this); 
    ms.GlobalMatch("(%d%d?%d?)", b); 
} 

void InterpreterClass::MatchAddressCallback(const char * match, const unsigned int length, const MatchState & ms) 
{ 
    //do something 
} 

2番目のパラメータが、私は文字列が解釈された後に実行するメソッドである、問題は、関数は、それが「委任」された場合のようなパラメータの特定のシーケンスに従う必要があるということです。

typedef void (*GlobalMatchCallback) (const char * match,   // matching string (not null-terminated) 
            const unsigned int length, // length of matching string 
            const MatchState & ms);  // MatchState in use (to get captures) 

すべてのパラメータが宣言され、その型名も宣言されたバインダーを実装しようとしました。

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I> 
struct bind_regex_member 
{ 
    typedef void(*fn_type)(const char *, const unsigned int, const MatchState &); 
    explicit bind_regex_member(const T* _ptr) 
    { 
     ptr = _ptr; 
    } 
    static void func(const char * match, const unsigned int length, const MatchState & ms) 
    { 
     (ptr->*PTR)(match, length, ms); 
    } 
    operator fn_type() 
    { 
     return &func; 
    } 
private: 
    static const T* ptr; 
}; 

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I> 
const T* bind_regex_member<T, PTR, I>::ptr = NULL; 

コンパイラが示していることを最初のエラーは次のとおりです:

Error: `Interpreter.cpp:7:80: error: could not convert template argument ‘&InterpreterClass::MatchAddressCallback’ to ‘void (InterpreterClass::*)(const char*, unsigned int, const MatchState&)’` 

のいずれかに動作していないGlobalMatchCallbackにバインダーを作るベローは、バインダーに従います。 MatchAddressCallbackに何をすればいいですか?

プロジェクトの最小コードレポ:https://github.com/rsegecin/RegexArduino.git

PS:このタイプの問題で自分自身を表現することは非常に困難でしたので、フィードバックは歓迎されます。

答えて

4

ここでの問題は、bind_regex_memberのptrがconst Tを指し、InterpreterClass :: MatchAddressCallbackのメソッドが非constであることです。このような Basicly:

InterpreterClass i; 
const InterpreterClass* myPtr = &i; 
MatchState myMs; 
myPtr->MatchAddressCallback("", 0, myMs); // OUCH! myPtr points to const T and MatchAddressCallback is non const member function 

bind_regex_memberにPTRからのconstを削除し、それが動作するはずです!

EDIT: Interpreter.hにおける第二の問題があります:

class Interpreter 
{ 
public: 
    void init(); 
    GlobalMatchCallback MatchAddressCallback; // <----------- HERE 
}; 

あなたはこのようなメソッドを宣言することはできません。 「最終的には」Interpreter.hは次のようになります。あなたが話したよう

#ifndef _INTERPRETER_h 
#define _INTERPRETER_h 

#include <Arduino.h> 
#include "Regexp.h" 

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I> 
struct bind_regex_member 
{ 
    typedef void(*fn_type)(const char *, const unsigned int, const MatchState &); 
    explicit bind_regex_member(T* _ptr) 
    { 
     ptr = _ptr; 
    } 
    static void func(const char * match, const unsigned int length, const MatchState & ms) 
    { 
     (ptr->*PTR)(match, length, ms); 
    } 
    operator fn_type() 
    { 
     return &func; 
    } 
private: 
    static T* ptr; 
}; 

template<class T, 

void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I> 
T* bind_regex_member<T, PTR, I>::ptr = NULL; 

class InterpreterClass 
{ 
public: 
    void init(); 

    void MatchAddressCallback(const char * match, const unsigned int length, const MatchState & ms); 
}; 

extern InterpreterClass Interpreter; 

#endif 
+0

を私がやったが、私はまだところで、私は、問題が何であったか言ったが、私はどのようなエラーコンパイル唾を投稿するのを忘れて、同じエラーを取得していますでる。エラー: 'Interpreter.cpp:7:80:エラー:テンプレート引数を変換することができませんでした '&InterpreterClass :: MatchAddressCallback' から 'のボイド(InterpreterClass :: *)(のconst char型*、unsigned int型、constのMatchState&)'' –

+1

は私が編集した私の回答! – Salco

+0

サルコありがとうございました。 –

関連する問題