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

I am learning C++ templates on vs2015 community.Here is my code, I want to define a template class and call the member function in the main() function.

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;
}

When I build this program, I get error at the last line. It says:

Expression preceding parentheses of apparent call must have (pointer-to-) function type

What should I do?

1

4 Answers

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

2

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 ...
  • 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;
}
0

This problem can also be caused if you are sending non-constant values into the max method.

This is the syntax for max:

constexpr const T& max (const T& a, const T& b, Compare comp);
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like