• Register
0 votes
1.6k 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

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 58 views
58 views
How c++ expression can have integral or unscoped type?
asked Sep 22 Daniel Anderson 4k points
1 vote
2 answers 642 views
642 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 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
class A { public: int m_count; int m_values[300]; void Method1(); } class B { void Method2(); } void A::Method1() { for(int i=0; i<m_count; i++) { creates instances of class B } } Problem] B::Method2() needs m_values array of class A for each ... B. Here m_values get update every minute by another function in class A. Is it possible to access m_values of class A in all instances of class B?
asked Sep 14 Marivoke 530 points
0 votes
1 answer 17 views
17 views
Problem: I am having only fundamental knowledge of g++. Now I am trying to compile my code using g++ and either the with the use of -std=c++11 or c++0x flags. But I am facing below error: cc1plus: error: unrecognized command line option "-std= ... . This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
asked Aug 11 Raphael Pacheco 4.9k points
0 votes
1 answer 31 views
31 views
Problem: I am having a sorted array and I need to remove the duplicates in-place in such a way that each of the element appears only once and then return a new length. For example nums = [1,1,2] a function should be returning [1,2]. Please find below my code for ... ]) {                 nums.erase(nums.begin() + i);             }             else i++;         }         return nums.size();     } };
asked Aug 14 Raphael Pacheco 4.9k points