Search code examples
c++templatesmathfloating-pointclass-template

Argument list for class template "calc" is missing


I am trying to modify the class “calc” to be more generic to accept also doubles or floats.

class calc {
public:
    int multiply(int x, int y);
    int add(int x, int y);
};

int calc::multiply(int k1, int k2)
{
    return k1 * k2;
}

int calc::add(int k1, int k2)
{
    return k1 + k2;
}

This is my implementation below, but I have an error E0441: argument list for class template "calc" is missing (line: calc c;).

template < class T>
class calc
{
public:

    T multiply(T x, T y);
    T add (T x, T y);
};

template < class T>
T calc<T>::multiply(T k1, T k2)
{
    return k1 * k2;
}

template < class T>
T calc<T>::add(T k1, T k2)
{
    return k1 + k2;
}

int main()
{
    calc c;
    std::cout << c.multiply(1, 5);
}

How do I convert the class to be a template class and function?


Solution

  • How is the compiler to know what kind of calc you want? You have to tell it:

    calc<int> c;
    

    or

    calc<double> c;
    

    or...