• Register
0 votes
2.8k 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 25 views
25 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 5.6k points
0 votes
1 answer 515 views
515 views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 1.6K views
1.6K 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 5 views
5 views
Problem: I'm trying to compile using g++ and either the -std=c++11 or c++0x flags. However, I get this error cc1plus: error: unrecognized command line option "-std=c++11" g++ --version g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) ... Foundation, Inc. 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 Dec 24, 2020 Mashhoodch 5.6k points
0 votes
1 answer 18 views
18 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, 2020 Marivoke 530 points
0 votes
1 answer 37 views
37 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, 2020 Raphael Pacheco 4.9k points
1 vote
2 answers 244 views
244 views
Problem: Hello all, How to declare an array properly in c/c++? I am a new student of c++ and I am trying to declare two-dimensional arrays in my program. Unfortunately, my arrays are not working. Here is my sample code int row = 5; int col= 5; int [row][col]; The above codes throw an error expression must have a constant value c++. What&rsquo;s wrong with it?
asked Apr 7, 2020 Gavin 15.3k points
0 votes
1 answer 151 views
151 views
Problem: I have decent knowledge in C++. Here I am trying to write the quicksort please find below my C++ code: void swap(int& a, int& b); int mid(int lo, int hi); // Below is my quicksort implementation void sort(int vec[], int lo, int hi) {         int ... = mid(lo, hi);                     ~~~^ 1 error generated. Everything seems to be correct. Can someone help me in fixing my above C++ code?
asked Jul 22, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 15 views
15 views
Problem: My problem is the following: I have a class Nodo, which has as an attribute an array of pointers toto its same type (as to make a tree) and then I have a child class NodoAvlthat must inherit that attribute but the pointers must point to the same type , that is, point to NodoAvl. ... = T ()): Node <T> (data) {} int altu () {return alt;} }; Is there a way to do this? Thanks in advance.
asked Jan 1 sasha 5.3k points
0 votes
1 answer 78 views
78 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, 2020 Raphael Pacheco 4.9k points