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;
}
An开发者_运维知识库d 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
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
Your multiple definition says template <class A_type>
(lower case "t"), then you use uppercase elsewhere.
精彩评论