• Register
0 votes
4.4k views

Problem :

I am very new to C++ and trying to learning C++ template. Please find below my code, I am trying to define a template class and also want to call the member function in the main() method..

template <typename T>

class Calculate {

    T _x;

    T _y;

    Calculate () {};

public

    Calculate (T x, T y) :_x(x), _y(y) {};

    T add const() { return _x + _y; };

    T sub const() { return _x - _y; };

};

int main() {

    Calculate <int> ar(7,8);

    cout << ar.add() << endl;

}

But whenever I try to build this program, I am getting following error at the last line of above code :

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

How can I resolve this issue?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

·   You need to add required header inclusion and using keywords

·    Also add : after public

·    Please move const to proper position

#include <iostream>

using std::cout;

using std::endl;

template <typename T>

class Calculate {

    T _x;

    T _y;

    Calculate () {};

public:

    Calculate (T x, T y) :_x(x), _y(y) {};

    T add() const { return _x + _y; };

    T sub() const { return _x - _y; };

};

int main() {

    Calculate <int> ar(7,8);

    cout << ar.add() << endl;

}

9 7 4
38,600 points

Related questions

0 votes
1 answer 57 views
57 views
Problem : How can I resolve this issue: Expression preceding parentheses of apparent call must have (pointer-to-) function type.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3 views
3 views
Problem: My program that gives this error is: #include <string> int main() { std::to_string(0); return 0; }
asked Mar 17 ummesalma 22k points
0 votes
1 answer 6 views
6 views
problem: I need someone to understand this problem and find out the solution... c++ enum class to int
asked Mar 26 Ifra 24.4k points
0 votes
1 answer 5 views
5 views
Problem: How to fix this error: Undefined reference to std::cout
asked Mar 16 ummesalma 22k points
0 votes
1 answer 6 views
6 views
Problem: I need your help , any solution: c++ expression must have class type
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 98 views
98 views
Problem: I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }
asked Dec 26, 2020 Mashhoodch 13k points
0 votes
1 answer 1K views
1K views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 2K views
2K views
Problem: I've got a problem with c++ code. I am attaching the sample code snippet below. Please pull me up. Yee, of course, I am pretty new in C++ #include <iostream> using namespace std; class Myclass { public: void myfunc() { cout<<"Hello World"<<endl; } }; int main ... = new Myclass(); // myfunc is part of the class object, rather than // part of the pointer to that class' object. a.myfunc(); }
asked Mar 7, 2020 Gavin 15.3k points
0 votes
1 answer 2 views
2 views
Problem: c++ std::forward.
asked Mar 24 Dan phillip 4.1k points
0 votes
1 answer 3 views
3 views
Problem: I need someone to understand this problem and find out the solution. Thanks .. C++ std::hash
asked Mar 22 Ifra 24.4k points