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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.