c++ – expression preceding parentheses of apparent call must have (pointer-to-) function type

c++ – expression preceding parentheses of apparent call must have (pointer-to-) function type

For anyone else this might also be because of redefinition of a method or property name. i.e a property and method might have the same name

The error indicates trying to call a function max() that is not defined as a function. Change parenthesis after const keyword to after the identifier max:

T max const()...

to

T max() const ...

c++ – expression preceding parentheses of apparent call must have (pointer-to-) function type

  • Add required header inclusion and using
  • Add : after public
  • Move const to proper position
#include <iostream>
using std::cout;
using std::endl;

template <typename T>
class Arithmetic {
    T _a;
    T _b;
    Arithmetic() {};
public:
    Arithmetic(T a, T b) :_a(a), _b(b) {};
    T max() const { return _a + _b; };
    T minus() const { return _a - _b; };
};

int main() {
    Arithmetic<int> ar(5,6);
    cout << ar.max() << endl;
}

Leave a Reply

Your email address will not be published.