Steve's a D
Steve's a D

Reputation: 3821

C++ templates - basics

I'm trying to follow my college notes, and I've tried googling the error and looking on stackover flow but I can't seem to figure out whats wrong.

I've read on so many places that you need to have both implementation and specification files in one file (header) so I've done so. I've just copy and pasted from my printed slides, and have googled this and tried copying exactly what was written on the page and still I get errors. I'm using g++ compiler.

Anyway, here is my code.

template<class A_Type> 
class calc
{
  public:
    A_Type multiply(A_Type x, A_Type y);
    A_Type add(A_Type x, A_Type y);
};

template<class A_type> 
A_Type calc<A_Type>::multiply(A_Type x, A_Type y)
{
  return x*y;
}
template<class A_Type> 
A_Type calc<A_Type>::add(A_Type x, A_Type y)
{
  return x+y;
}

And I get the error: expected constructor, destructor, or type conversion before 'calc' (on line 10 of test.h)

Am I missing something? I dont get it

Upvotes: 2

Views: 158

Answers (2)

Tony Delroy
Tony Delroy

Reputation: 106244

Your multiple definition says template <class A_type> (lower case "t"), then you use uppercase elsewhere.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355357

template<class A_type>                            // lowercase t in A_type
A_Type calc<A_Type>::multiply(A_Type x, A_Type y) // uppercase T's in A_Type

Upvotes: 5

Related Questions