Hi

I'm trying to use a vector to store function pointers, but when implemented in a class there method ceases to work.

Class header:

std::vector<void (*)()> fns

Class .cpp file:

fns.push_back(Search);
fns.push_back(Print);

Search() and Print() are (public) members of a class named Movie. This code is currently placed in the constructer. But wont compile.
The error is:

error C3867: 'Movie:: Search': function call missing argument list; use '&Movie:: Search' to create a pointer to member

I tried to do that, but then I get a mismatch compile error.

Any ideas?

Also, should I create a new thread with a question of a way to have different kind of function pointers (different return types and arguments) in a single vector?

Thanks.

Recommended Answers

All 6 Replies

this might be one way to resolve the different parameters problem. Pass the parameters you want as the seccond parameter and use stdargs.h to retrieve them. If you don't want to do that, then you might get away with using templates, but someone else will have to answer that question.

#include <iostream>
#include <vector>
#include <string>
#include <stdarg.h>
using namespace std;

void A(char* fmt, ...)
{

}

void B(char* fmt, ...)
{

}

int main()
{
    std::vector<void (*)(char* fmt, ...)> fns;
    fns.push_back(A);
    fns.push_back(B);
}

To invoke function of a class through pointers, you may have to use a pointer to a member function.

Your vector will have to store MtFP's instead of regular FP's.

Thanks, but it didn't work yet.

std::vector<void (Movie::*)> fns;

But now: error C2182: 'abstract declarator' : illegal use of type 'void'.

and this one is the same but have more detail:
'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'void (__thiscall Movie:: * )(void)' to 'int Movie:: * const &'

fns.push_back(&Movie::Search);

Here's a working example--

#include <iostream>
#include <vector>

class MyClass{

    public:
            std::vector< void(MyClass::*)()> ptmf_Vector;

            MyClass(){
                ptmf_Vector.push_back(&MyClass::Search);
                ptmf_Vector.push_back(&MyClass::Print);
            }
            void Search(){std::cout << "search" << std::endl;}
            void Print(){std::cout << "print" << std::endl;}
};

int main(){
    MyClass mc;
    std::vector<void (MyClass::*)()> temp = mc.ptmf_Vector;
    (mc.*temp[0])();
    return 0;
}

--sorry I couldn't post this earlier but I had similar problems and looked into the suggestion i posted more thoroughly.

As usually, a proper typedef helps to clear codes:

class Movie
{
    typedef void (Movie::*MovieFunc)();
public:
    Movie()
    {
        f.push_back(&Movie::heHe);
        f.push_back(&Movie::haHa);
    }
    void show(int part) { (this->*f[part])(); }
private:
    void heHe() { cout << "He he" << endl; }
    void haHa() { cout << "Ha ha" << endl; }

    vector<MovieFunc> f;
};

void Serial()
{
    Movie m;
    m.show(0);
    m.show(1);
}
commented: I agree, that's much more readable! +3

Both of you, thank you very much :) A working and very readably solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.