template class as a member

Hi

I get this error:

'MyClass' use of class template requires template argument list

My template class is as follows:

template <class T=int> class MyClass{
:
:
}

Another class has this template class as members.

#include "MyClass.h"
class B{
     private:
          MyClass a; //This line gives me the above mentioned error
    :
    public:
         B(MyClass in): a(in) {}
}

what am i doing wrong?

I wanted to do something like this:
MyClass<T>  a;


where the type of a i.e. the member  would be defined by what's passed in to the constructor. Is this possible?
 Can the type of a member be a template class, whose type parameter (T=int or float etc) be defined at compile time by what's passed in the constructor?
LuckyLucksAsked:
Who is Participating?
 
jkrCommented:
Ooops, that was simultaneous ;o)

Anyway, further explanations here: http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part#DefArgClassTempl ("Default Template Arguments with Class Templates")
0
 
ZoppoCommented:
Hi LuckyLucks,

IMO you need to add template braces even if you want use default type - try to declare the member like this:
MyClass <> a;

Open in new window

Hope that helps,

ZOPPO
0
 
jkrCommented:
You need to use '<>' to indicate that you are referring to the default type, i.e.

class B{
     private:
          MyClass<> a; // <--- !
};

Open in new window

0
 
ZoppoCommented:
... and, of course, if you want it for another type you can declare it too like i.e.:
MyClass <double> b;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.