0

I am struggling to fix and understand this simple code reported as erroneous by the compiler which is supposed to define the call operator for Klass:

template<typename T>
class Klass{
    T operator(){ return T; }
};

g++ 7.3 reports:

a.cpp:3:26: error: declaration of ‘operator()’ as non-function
  T operator(){ return T; }
                          ^
a.cpp:3:26: error: expected ‘;’ at end of member declaration

What is wrong?

eudoxos
  • 18,545
  • 10
  • 61
  • 110
  • 1
    .`operator()()` – hellow Sep 03 '18 at 15:15
  • 3
    `T operator()(){ return T; }` But you then have another problem with `return T`, which is a type, not a value. –  Sep 03 '18 at 15:15
  • The form of declaring an `operator()` is `operator()(arg_types)`. For example, if you want to call `object(2)`, `operator()` needs to be `T operator()(int x)`. – Peter Sep 03 '18 at 15:15
  • 1
    In the marked duplicate, look for "function call operator". There are plenty of unrelated operators described. – Peter Sep 03 '18 at 15:19

0 Answers0