3

boost::python を使用してオーバーロードされた関数を公開しようとしています。関数プロトタイプは次のとおりです。

#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);

この回答を見てきました:オーバーロードされた関数へのポインターを指定するにはどうすればよいですか?
これを行う:

BOOST_PYTHON_MODULE(python_bridge)
{
    class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
        .def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
    ;
}

次のエラーが発生します。

error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
      None of the functions with this name in scope match the target type

以下を試してください:

void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;

結果:

error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
          None of the functions with this name in scope match the target type

何が問題で、どうすれば修正できますか?

編集 私はいくつかのことを言及するのを忘れていました:

  • win-7でvs2010 proを使用しています
  • write は logical_file のメンバ関数です
  • FMS_logic は名前空間です
4

2 に答える 2

2

write が純粋な関数である場合、2 番目の試行は機能するはずです。あなたのコードから、メンバー関数があるようです。メンバー関数へのポインターは醜いので、関数オブジェクトを使用したほうがよいでしょう。ただし、コード全体を投稿する必要があります。書き込みがメンバー関数であるかどうかは明確ではありません。

編集: FMS_logic::logical_file のメンバー関数である場合、構文は次のようになります。

void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;

これは、非静的メンバー関数にのみ適用されます。つまり、関数が静的であるか、logical_file が単なる名前空間である場合は、以前に記述したとおりです。

于 2013-07-25T08:29:46.327 に答える