1

Possible Duplicate:
How to call a function using pointer-to-member-function

Analyzer.h

class Analyzer
{
public :

    void viku();
    void Bibek();
    void vivek();
    void (Analyzer::*point)();

    Analyzer(){

    }
    ~Analyzer(){

    }

};

Analyzer.cpp

    using namespace std     
    #include"Analyzer.h"
    void Analyzer::viku(){
        cout<<"Hello viku";
    }
    void Analyzer::vivek(){
        point =&Analyzer::viku; 
        Bibek();   
    }
    void Analyzer::Bibek(){
           point();//Errror
        cout<<"Bibek";
    }

During compilation it shows the following error:

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

Can anyone please tell me how to avoid this?

11
  • 2
    You should read the chapter in your book about pointers-to-member-functions, because that is not a normal function pointer. -1 for no prior research. Commented Dec 28, 2012 at 11:57
  • I read it but still not able to find out that's why i have posted this here . According to me i have done everything correct 1-both the function pointer and function's signature is same . and the calling convention too . Then where i am getting the problem ? Commented Dec 28, 2012 at 12:04
  • 1
    not related to your problem, but important: Avoid using using namespace in header files, better yet: don't use it at all. It might cause naming conflicts. Commented Dec 28, 2012 at 12:18
  • 2
    @LightnessRacesinOrbit: It looks like he understands the difference, but not that point(); doesn't work from a member function where viku(); would. +1 for a reasonable question. Commented Dec 28, 2012 at 12:18
  • 1
    @MikeSeymour: Please hover over the downvote icon and notice that lack of research is a reason to downvote. How to use pointers-to-member-functions is covered in any good C++ book and thus should not need to be asked again here. Or, if nothing else, it's a duplicate. Commented Dec 28, 2012 at 12:26

1 Answer 1

3

Pointers to member functions are different than normal function pointer. You need an instance to call them:

#include <iostream>

class A
{
public:
  int foo()
  {
      std::cout << "A::foo here, you can have 42" << std::endl;
      return 42;
  }
};

int main ()
{
  int (A::* point)() = &A::foo;
  A a;

  (a.*point)();
}

In your case, you'd need to do something like the following:

(this->*point)()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.