2012-02-20 13 views
0

GuiObjectクラスから派生したGUIオブジェクトがシリアライザとデシリアライザを登録できるシステムを設定しようとしています。シリアライザとデシリアライザは、クラスエイリアス。たとえば:ブーストを使用したメンバ関数のバインド

class Button : public GuiObject 
{ 
    // ... 
}; 

ButtonはXMLで次のようになります。

<button> 
    [...] 
</button> 

だから、アイデアは、クライアントコードは、プレースホルダを使用して派生GuiObjectためのシリアライザとデシリアライザを登録することができるということです上の関数を呼び出すオブジェクト:

typedef boost::function<void(ticpp::Element*)> serialisation_function; 

// ... 

serialisation_function serialiserFunc(boost::bind(&Button::WriteElement, _1, _2)); 
GuiObjectXmlSerialiser buttonSerialiser("button", serialiserFunc); 
guiObjectXmlFactory.registerSerialiser(buttonSerialiser); 

Button::WriteElementは次のようになります:

virtual void WriteElement(ticpp::Element* element) const; 

GuiObjectXmlSerialiser.h:

typedef boost::function<void(ticpp::Element*)> serialisation_function; 

// Serialises a GuiObject via a creation and serialisation function, storing 
// a class name to differentiate it between others. 
class GuiObjectXmlSerialiser 
{ 
public: 
    GuiObjectXmlSerialiser(const std::string& class_alias, 
     serialisation_function serialisation_func); 

    void serialise(gui_object_ptr object, ticpp::Element* element); 
private: 
    std::string class_alias; 
    serialisation_function serialisation_func; 
}; 

GuiObjectXmlSerialiser.cpp:

error C2064: term does not evaluate to a function taking 2 arguments 

GuiObjectXmlSerialiser::GuiObjectXmlSerialiser(const std::string& class_alias, 
serialisation_function serialisation_func) 
: class_alias(class_alias) 
, serialisation_func(serialisation_func) 
{ 
} 

void GuiObjectXmlSerialiser::serialise(gui_object_ptr object, ticpp::Element* element) 
{ 
    serialisation_func(object, element); 
} 

現在、私はserialisation_func(object, element);ラインでこのエラーを取得していますだから私は、メンバー関数を呼び出すためにオブジェクトを渡すのを遅らせる方法を知りたいと思います。関数を呼び出すのと同時にオブジェクトを指定してください。私はしばらくの間これを見つめていませんでした。

乾杯。

答えて

0

私は2つのエラー見ることができます:あなたは、単一の引数を取る関数を定義していますが、二つの引数でそれを呼び出そう

まず:

// Single argument of type ticpp::Element* 
typedef boost::function<void(ticpp::Element*)> serialisation_function; 
// Call with two arguments: 
serialisation_func(object, element); 

// You probably meant: 
typedef boost::function<void(gui_object_ptr, ticpp::Element*)> serialisation_function; 

第2に、関数がメンバー関数であることを考慮しません。つまり、実際には3番目のパラメータthis(最初の位置)が使用されています。これがメンバー機能であるべきかどうかをチェックするべきですが、あなたの理解に間違いがあると思います。

// From your code I am not sure what [serial_instance] should be. 
serialisation_function serialiserFunc(boost::bind(&Button::WriteElement, [serial_instance] , _1, _2)); 
+0

2つのパラメータでコードを呼び出すと、コード内のその時点で関数を呼び出すようにオブジェクトをバインドし、例のように 'function'を作成するのではなく、バインドする必要があることを明確にしようとしていたためです。 '_1'(' serialisation_function'の構築中)は、メンバ関数を呼び出すオブジェクトであると考えられます。だから私が知りたいのは、関数が呼び出されるまでオブジェクトのバインドを遅らせる方法です。振り返ってみると、私の質問は少しはっきりしていた可能性があります。 – Mitch

+0

仮想関数では危険ですが、次のように試してみてください。最初に、あなたの関数に 'typedef boost :: function serialisation_function;という2つのパラメータをとり、特別な処理をせずにバインドします:' serialisation_function serialiserFunc(&Button :: WriteElement); '。私は申し訳ありませんが、今は時間がないことをテストしませんでした。 –

+0

ありがとう!これにより、エラーが停止しました。 – Mitch

0

あなたが好きななめらか、ウィッヒのためにあなたが結合呼び出したい、クラスオブジェクトのコピーを指示する必要があります:あなたは、このクラスのオブジェクトのコピーのためのバインディングを作成したい場合も、あなたは、thisポインタを使用することができます

Button* btn = new Button(); 
serialisation_function serialiserFunc(boost::bind(&Button::WriteElement, btn, _1, _2)); 

+0

私の質問は、どのようにオブジェクトを呼び出すかを示すのが遅れていますか? – Mitch

+0

私の例では、クラスオブジェクトのコピーにboost :: bindポインタの2番目のパラメータを渡します。 – Olympian

+0

私はあなたが私の質問を誤解していると思っています。私がやろうとしていることを明確にするためにJ.N.の最後のコメントをご覧ください。 – Mitch

関連する問題