template question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Milan

    template question

    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.


  • Victor Bazarov

    #2
    Re: template question

    "Milan" <someone@intern et.com> wrote...[color=blue]
    > 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" <<[/color]
    endl;}[color=blue]
    > };
    >
    > 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;}[/color]

    You cannot construct A<X> here, it's not a member. You can only
    construct A<int>.
    [color=blue]
    > };
    >
    > 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;}[/color]

    Same here. You cannot construct A<X>. You can only construct
    A<double>.
    [color=blue]
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > B obj1;
    > C obj2 = B;[/color]

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

    C obj2 = obj1;

    ?
    [color=blue]
    > return 0;
    > }
    >
    > //////////////////////////////////////////////
    >
    > It fails to compile on the "C obj2 = B" line. Why?[/color]

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

    Victor


    Comment

    • hrmadhu

      #3
      Re: template question

      // try
      C obj2=obj1
      // B is a type...not an object.
      // Madhu.


      "Milan" <someone@intern et.com> wrote in message news:<bo5t9f$1c 01@imsp212.netv igator.com>...[color=blue]
      > 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.[/color]

      Comment

      • Milan

        #4
        Re: template question

        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" <v.Abazarov@com Acast.net> wrote in message
        news:CUvpb.7005 6$mZ5.434158@at tbi_s54...[color=blue]
        > "Milan" <someone@intern et.com> wrote...[color=green]
        > > int main(int argc, char* argv[])
        > > {
        > > B obj1;
        > > C obj2 = B;[/color]
        >
        > 'B' is a type name. The compiler expects an expression. Did
        > you mean to write
        >
        > C obj2 = obj1;
        >
        > ?
        >[color=green]
        > > return 0;
        > > }[/color][/color]


        Comment

        • Victor Bazarov

          #5
          Re: template question

          "Milan" <someone@intern et.com> wrote...[color=blue]
          > 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'[/color]
          :[color=blue]
          > illegal member initialization: 'A<int>' is not a base or member". How do I
          > resolve this? Many thanks.[/color]

          By getting a more standard-compliant compiler.

          Victor


          Comment

          Working...