template question

M

Milan

I need some help on the following simple program:

//////////////////////////////////////////////
template <class T>
class A
{
public:
T x, y;
A(){x = y = 0;}
A(T x_, T y_):x(x_), y(y_){}
template <class X> A(const A<X> &a){x = a.x; y = a.y; cout << "A" << endl;}
};

class B: public A<int>
{
public:
B(){}
B(int x_, int y_): A<int>(x_, y_){}
template <class X> B(const A<X> &a): A<X>(a){cout << "B" << endl;}
};

class C: public A<double>
{
public:
C(){}
C(double x_, double y_): A<double>(x_, y_){}
template <class X> C(const A<X> &a): A<X>(a){cout << "C" << endl;}
};

int main(int argc, char* argv[])
{
B obj1;
C obj2 = B;
return 0;
}

//////////////////////////////////////////////

It fails to compile on the "C obj2 = B" line. Why?

Thanks for the help.

Regards,
Milan.
 
V

Victor Bazarov

Milan said:
I need some help on the following simple program:

//////////////////////////////////////////////
template <class T>
class A
{
public:
T x, y;
A(){x = y = 0;}
A(T x_, T y_):x(x_), y(y_){}
template <class X> A(const A<X> &a){x = a.x; y = a.y; cout << "A" << endl;}
};

class B: public A<int>
{
public:
B(){}
B(int x_, int y_): A<int>(x_, y_){}
template <class X> B(const A<X> &a): A<X>(a){cout << "B" << endl;}

You cannot construct A<X> here, it's not a member. You can only
construct A said:
};

class C: public A<double>
{
public:
C(){}
C(double x_, double y_): A<double>(x_, y_){}
template <class X> C(const A<X> &a): A<X>(a){cout << "C" << endl;}

Same here. You cannot construct A<X>. You can only construct
A said:
};

int main(int argc, char* argv[])
{
B obj1;
C obj2 = B;

'B' is a type name. The compiler expects an expression. Did
you mean to write

C obj2 = obj1;

?
return 0;
}

//////////////////////////////////////////////

It fails to compile on the "C obj2 = B" line. Why?

Doesn't your compiler tell you why? If it doesn't, how do you
know it fails?

Victor
 
M

Milan

Sorry, I made a mistake. The expression should be

C obj2 = obj1;

However it still fails to compile with an error message "error C2614: 'C' :
illegal member initialization: 'A<int>' is not a base or member". How do I
resolve this? Many thanks.

Regards,
Milan.


Victor Bazarov said:
Milan said:
int main(int argc, char* argv[])
{
B obj1;
C obj2 = B;

'B' is a type name. The compiler expects an expression. Did
you mean to write

C obj2 = obj1;

?
return 0;
}
 
V

Victor Bazarov

Milan said:
Sorry, I made a mistake. The expression should be

C obj2 = obj1;

However it still fails to compile with an error message "error C2614: 'C' :
illegal member initialization: 'A<int>' is not a base or member". How do I
resolve this? Many thanks.

By getting a more standard-compliant compiler.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top