2

I've read this question How to make a function return a pointer to a function? (C++)

...but I am still having a problem. The Index function returns an enumerator function that takes a function to which it yields each index. The function signatures have been typedefed in Indexer.hpp:

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

...and the Indexer class

// 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;
}

The compiler fails with the error below:

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*))}'

What am I missing in my declarations?

Community
  • 1
  • 1
IAbstract
  • 19,551
  • 15
  • 98
  • 146
  • 1
    I have to ask .... why are you making life so difficult for yourself here? What's with all the function pointers? – Lightness Races in Orbit Jan 07 '17 at 18:09
  • 2
    A pointer to a non-member function is not the same as a pointer to a member function. The difference being that a pointer to a non-member function doesn't need an object to be called on, but a pointer to a member function does. I suggest you read about [`std::function`](http://en.cppreference.com/w/cpp/utility/functional/function) and [`std::bind`](http://en.cppreference.com/w/cpp/utility/functional/bind). – Some programmer dude Jan 07 '17 at 18:11
  • Also you should know by now that we expect an [MCVE] here. You have a ton of unmentioned types in this snippet. – Lightness Races in Orbit Jan 07 '17 at 18:11
  • `enumerate_indexes` is not a function. It is a class method. Big difference. That's your problem. The correct answer depends on the details that are missing from the question, hence a [mcve] is required. – Sam Varshavchik Jan 07 '17 at 18:13
  • It seems I've found how to get it to compile. Thanks to @Someprogrammerdude for pointing me in the right direction with "pointer to a non-member function is not the same as a pointer to a member function." I'll post my own answer as the *ton of unmentioned types* could just be `int`s or whatever and completely irrelevant. – IAbstract Jan 07 '17 at 18:33

1 Answers1

0

In Indexer.hpp the typedef needs to tell the compiler that enumerator is an Indexer member method:

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

Now, other classes calling the Indexer::Index(..) is:

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

bool do_something_with_index(Index* idx) {
   return condition = true; // some conditional logic
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146