-3

Can anyone help me with the following:

  class Bix
 {
    public:
    static void MyFunction();
 };
 int main()
 {
   void(*ptr)() = &Bix::MyFunction;
   return 0; 
  }

This shows linker error!!! Why?

user3020345
  • 19
  • 2
  • 9
  • 3
    What does the error say? Is this really the entirety of the code? If not, where is the definition of `MyFunction()`? – user1118321 Jun 29 '14 at 00:59
  • The answer options were: a)The program reports an error as pointer to member function cannot be defined outside the definition of class. b)The program reports an error as pointer to static member function cannot be defined. c)The program reports an error as pointer to member function cannot be defined without object. d) The program reports linker error. The answer given was d) – user3020345 Jun 29 '14 at 01:08
  • @user3020345 Which _answer options_ are you talking about please?!? The answer is: You're missing a definition for `static void MyFunction();` from what you're actually showing, period! – πάντα ῥεῖ Jun 29 '14 at 01:45

1 Answers1

2

Bix::MyFunction is declared but never defined. Try

 class Bix
 {
    public:
    static void MyFunction() { printf("Bix::MyFunction"); }
 };

See here for a working variant.

Matt Phillips
  • 9,465
  • 8
  • 44
  • 75
  • Thank you. Was that the only reason why it showed linker error – user3020345 Jun 29 '14 at 01:06
  • The answer options were: a)The program reports an error as pointer to member function cannot be defined outside the definition of class. b)The program reports an error as pointer to static member function cannot be defined. c)The program reports an error as pointer to member function cannot be defined without object. d) The program reports linker error. – user3020345 Jun 29 '14 at 01:06
  • @user3020345 Yes, since the error went away with only that change. As for your options, consider which of them state that `void(*ptr)...` should *never* work, and see that you can rule those out now that a simple change got the code to link. What's left is the answer. – Matt Phillips Jun 29 '14 at 01:17