开发者

Passing member function to function pointer

I have to pass in a function pointer of a member function to another class. I couldn't get it to compile and I get the "undefined reference to classB::classB(classA::*)(std::shared_ptr) " error. Can someone help me with this?

Thanks.

classA{
  string functionA(share_ptr<int>);
  void functionB(){
     classB* ptr开发者_Go百科 = new classB(&classA::functionA);
  }
}

//in other .h file
classA; //forward declaration

classB{
  classB(string (classA::*funcPtr)(shared_ptr<int>); //constructor
}


Your code as I'm writing this:

classA{
  string functionA(share_ptr<int>);
  void functionB(){
     classB* ptr = new classB(&classA::functionA);
  }
}

Here:

  • share_ptr should be shared_ptr.

  • There is a missing semicolon at the end of the class definition.

Then,

//in other .h file
classA; //forward declaration

classB{
  classB(string (classA::*funcPtr)(shared_ptr<int>); //constructor
}

Here:

  • There are three left parentheses ( but only two right parentheses ).

  • A semicolon is missing at the end of the class definition.

You're asking about a linking error, but your code should not even compile.

When you state "I couldn't get it to compile" that seems to be correct.

When you then state "I get [undefined reference to...]", well that's a mystery: with the toolchain that you're evidently using, the linker shouldn't be invoked when the compilation fails.

In summary, this question contains some incorrect information and any answer (such as the hypothesis that you haven't defined the function, or the hypothesis that you defined it somewhere but forgot to link in that, or the hypothesis that you're reporting errors from building something else than your code, so on) would be pure guesswork.

Please post a complete small program that compiles and illustrates the linking error.

Cheers & hth.,


Just for giggles, going to answer this even though you said it compiled for you ...

// In a.hpp
#include "b.hpp" // you need to include this because you will not
                 // be able to call B's constructor in your functionB() method

class A
{
public:
  string functionA ( shared_ptr<int> ); // IIRC, needs to be public to call it
                                        // from B's constructor.

  // can be public/protected/private depending on where it's called ...
  void functionB () {
    B * ptrB = new B ( &A::functionA );
  }
};

and

// In b.hpp

// Forward declare class A
class A;
// To make the constructor of B cleaner, use a typedef.
typedef string (A::*IntPtrToStrFn) ( shared_ptr<int> );

class B
{
public:
  B ( IntPtrToStrFn fnptr );
};

As far as style and code readability goes, this is horrible - ties the two classes together and reaks of code smell. Need to look at some kind of adapter class or other design pattern to get the two classes to work together.


On the surface, this just looks like a forward declaration issue. Try declaring classA::functionB as you've done, and then defining classA::functionB after the definition of classB.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜