0

gcc 6.4.0 win 7 Netbeans 8.2

I can't figure what I'm doing wrong.

#ifndef TYPEDEF_H
#define TYPEDEF_H

class Typedef {
   class Alphabet { };
   class Tuple { };
   class Operations { };
public:
   void test();
private:
   Tuple constant(long one, long two, Alphabet& bet, Operations& op) { return Tuple(); } ;
   Tuple expression(long one, long two, Alphabet& bet, Operations& op) {return Tuple(); } ;
};

#endif /* TYPEDEF_H */

===================== .cpp file ========================
#include "Typedef.h"

void Typedef::test() {
   typedef Tuple (*fn)(long, long, Alphabet&, Operations&);
   fn func[]      = { constant, expression };
   Tuple (*fnc[2])= { constant, expression };
}

Typedef.cpp: In member function 'void Typedef::test()':

Typedef.cpp:6:44: error: cannot convert 'Typedef::constant' from type 'Typedef::Tuple (Typedef::)(long int, long int, Typedef::Alphabet&, Typedef::Operations&)' to type 'fn {aka Typedef::Tuple (*)(long int, long int, Typedef::Alphabet&, Typedef::Operations&)}' fn func[] = { constant, expression };

The error message is repeated for all four instances. I've tried &constant and as expected it didn't work.

2
  • Member functions aren't functions... Commented Sep 29, 2017 at 14:44
  • You may be interested in std::function. Commented Sep 29, 2017 at 14:44

1 Answer 1

2

A class member function is special in that it accepts an additional invisible argument, the this* pointer. Also, a member function and a free function are completely different types, even if the latter had the extra argument compatible with the this* type.

To capture a pointer to the member function, use the member pointer syntax (Typedef::*), like this:

void Typedef::test() {
  typedef Tuple (Typedef::*fn)(long, long, Alphabet&, Operations&);
  fn func[] = { &Typedef::constant, &Typedef::expression };
}

In addition, an address of a member function can be taken only on fully-qualified names (reference: [expr.unary.op]/4), so you have to use &Typedef::constant, &Typedef::expression.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I don't understand why the Typdef:: is required since the functions are visible.
@ArthurSchwarez see my updates. Typedef::* here is not for visibility but a member pointer syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.