Templates+inheritance

Hey all,

When I compile my program I get 19 same errors that say : error C4519: default template arguments are only allowed on a class template.

for every class in the program there is a tempalte and every class inherits from other classes.

Header code example :


template<class T> class Point
{
public:
T x;
T y;
Point(T _x=0,T _y=0): x(_x),y(_y){}
void move(T _x,T _y)
{
x+=_x;
y+=_y;
}

};
class Shape
{
public:

virtual void print()=0;
virtual Shape* clone()=0;
virtual void extend(int i)=0;
virtual void move(int x,int y)=0;
};


template<class X, class T=Point<X>>
class Circle:public Shape
{
T center;

public:

X r1;
Circle();
Circle(Point<X>, X r1);
Circle(X r1,X x,X y);
void move(int x,int y);
void print();
void extend(int i);
Shape* clone();
};


This is one of main code functions that gives that error message :

template<class X,class T=point<X> >
Circle<X,T>::Circle(X a, X b,X c)
{
center=Point<X>(b,c);
r1=a;
}


I have to use templates and inheritance !!

Please help... Thank u
closed account (zb0S216C)
maroun wrote:
template<class X, class T=Point<X>>

This needs to be:

template <class X, template <typename> class T>

Wazzak
Last edited on
Remove the default template argument from the function definition.
@Framework what do you mean in <typename> ?? int , char .. ??

Point is also a function , and I want the type of its variabels to be decided by X.
And then I can declare a variable 'center' for example : T center; (is a type of point that its variables are type of X).

@Peter87 if I understand u correctly and I'll remove the default template, i'll have a problem with undeclared Type X for function variables , as well as with center in the function.
Last edited on
closed account (zb0S216C)
maroun wrote:
"what do you mean in <typename> ??"

It informs the compiler to expect a class template for the second template parameter.

maroun wrote:
"Point is also a function"

Erm... No, it's not; it's a class.

Wazzak
Thank you !! :)
But then I need to send in main the exact template and I can't use for example this main: Circle<char>*ch=new Circle<char>(2,3,3);
right ??
closed account (zb0S216C)
maroun wrote:
"But then I need to send in main the exact template and I can't use for example this main"

I'm sorry?

That code should compile. Though, I think you're confusing constructors with functions.

Wazzak
Topic archived. No new replies allowed.