2017-01-07 14 views
2

私はこの質問を読んだことがありますHow to make a function return a pointer to a function? (C++)どのように関数が関数をとる関数へのポインタを返すことができますか?

...しかし、私はまだ問題があります。 Index関数は、各インデックスを生成する関数をとる列挙関数を返します。

typedef bool (*yield)(Core::Index*); 
typedef int (*enumerator)(yield); 

...と、コンパイラは以下のエラーで失敗しIndexerクラス

// Indexer.hpp 
class Indexer { 

    public: 
     enumerator Index(FileMap*); 

    private: 
     int enumerate_indexes(yield); 
}; 

// Indexer.cpp 

enumerator Indexer::Index(FileMap* fMap) { 
    m_fmap = fMap; 
    // ... 

    return enumerate_indexes; 
} 

int Indexer::enumerate_indexes(yield yield_to) { 
    bool _continue = true; 

    while(_continue) { 
     Index idx = get_next_index();   
     _continue = yield_to(&idx); 
    } 

    return 0; 
} 

:何午前

Indexer.cpp: In member function 'int (* Indexer::Index(FileMap*))(yield)': 
Indexer.cpp:60:12: error: cannot convert 'Indexer::enumerate_indexes' from 
type 'int (Indexer::)(yield) {aka int (Indexer::)(bool (*)(Core::Index*))}' to 
type 'enumerator {aka int (*)(bool (*)(Core::Index*))}' 

関数のシグネチャは、typedef Indexer.hppでエドとなっています私の宣言には欠けていますか? 、今Indexer::Index(..)を呼び出して、他のクラスである

typedef int (Indexer::*enumerator)(yield); 

+1

私が聞いています...なぜあなたはここで自分の人生を難しくしていますか?すべての関数ポインタには何がありますか? –

+2

非メンバ関数へのポインタは、メンバ関数へのポインタと同じではありません。違いは、非メンバ関数へのポインタにはオブジェクトを呼び出す必要はありませんが、メンバ関数へのポインタが必要であるという点です。私はあなたが['std :: function'](http://en.cppreference.com/w/cpp/utility/functional/function)と[' std :: bind'](http:// en。 cppreference.com/w/cpp/utility/functional/bind)。 –

+0

また、ここで[MCVE]が必要であることがわかっているはずです。このスニペットには、記載されていない種類のトンがあります。 –

答えて

0

Indexer.hpptypedefenumeratorIndexerメンバーメソッドでコンパイラに指示する必要が

enumerator indexer = p_indexer->Index(&fmap); 
indexer(do_something_with_index); 

bool do_something_with_index(Index* idx) { 
    return condition = true; // some conditional logic 
} 
関連する問題