What the assignment operator from other type(with template) doesn't work?

Discussion in 'C++' started by PengYu.UT@gmail.com, Nov 9, 2005.

  1. Guest

    The following shows the source code and the error message(from
    g++-3.3). I wrote two assignment operator. One is for the same type
    case, the other one is for different type case. I'm wondering why it
    doesn't work. The error line is marked with comments in the source
    code.

    Thanks,
    Peng


    /* main.cc */
    template <typename U>
    class A{
    public:
    A() { }
    A &operator=(A &that) {
    _internal = that._internal;
    }
    template <typename V>
    A &operator=(V &that) {
    _internal = that.get_internal();
    }
    private:
    U _internal;
    };

    int main(int argc, char *argv[])
    {
    A<int> a;
    A<double> b = a;//error
    }

    /* error message */
    g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
    main.cc: In function `int main(int, char**)':
    main.cc:19: error: conversion from `A<int>' to non-scalar type
    `A<double>'
    requested
    make: *** [main.o] Error 1
    , Nov 9, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > The following shows the source code and the error message(from
    > g++-3.3). I wrote two assignment operator. One is for the same type
    > case, the other one is for different type case. I'm wondering why it
    > doesn't work. The error line is marked with comments in the source
    > code.


    Sorry, there were some typos in the previous post. Here is the
    corrected code. Please look at the error line at the end of the
    program.
    I'm wondering why
    A<double> b;
    b = a;
    works, but
    A<double> c = a; //error
    doesn't work.


    /** source code */
    template <typename U>
    class A{
    public:
    A() { }
    A &operator=(A &that) {
    _internal = that._internal;
    }
    template <typename V>
    A &operator=(V &that) {
    _internal = that.get_internal();
    }
    U get_internal() const {return _internal; }
    private:
    U _internal;
    };

    int main(int argc, char *argv[])
    {
    A<int> a;
    A<double> b;
    b = a;
    A<double> c = a; //error
    }


    /********* error message */
    g++-3.3 -g -I/usr/local/include/clapack/ -c -o main.o main.cc
    main.cc: In function `int main(int, char**)':
    main.cc:22: error: conversion from `A<int>' to non-scalar type
    `A<double>'
    requested
    make: *** [main.o] Error 1
    , Nov 9, 2005
    #2
    1. Advertising

  3. Kai-Uwe Bux Guest

    wrote:

    >
    > wrote:
    >> The following shows the source code and the error message(from
    >> g++-3.3). I wrote two assignment operator. One is for the same type
    >> case, the other one is for different type case. I'm wondering why it
    >> doesn't work. The error line is marked with comments in the source
    >> code.

    >
    > Sorry, there were some typos in the previous post. Here is the
    > corrected code. Please look at the error line at the end of the
    > program.
    > I'm wondering why
    > A<double> b;
    > b = a;
    > works, but
    > A<double> c = a; //error
    > doesn't work.
    >
    >
    > /** source code */
    > template <typename U>
    > class A{
    > public:
    > A() { }
    > A &operator=(A &that) {
    > _internal = that._internal;
    > }
    > template <typename V>
    > A &operator=(V &that) {
    > _internal = that.get_internal();
    > }
    > U get_internal() const {return _internal; }
    > private:
    > U _internal;
    > };
    >
    > int main(int argc, char *argv[])
    > {
    > A<int> a;
    > A<double> b;
    > b = a;
    > A<double> c = a; //error


    Contrary to what the notation suggests, this does not call the assignment
    operator. Instead a copy constructor is needed: C++ considers this line as
    the construction of the object c from the object a. The above is actually
    equivalent to:

    A<double> c ( a );

    > }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Nov 9, 2005
    #3
  4. wrote:
    > wrote:
    >> The following shows the source code and the error message(from
    >> g++-3.3). I wrote two assignment operator. One is for the same type
    >> case, the other one is for different type case. I'm wondering why it
    >> doesn't work. The error line is marked with comments in the source
    >> code.

    >
    > Sorry, there were some typos in the previous post. Here is the
    > corrected code. Please look at the error line at the end of the
    > program.
    > I'm wondering why
    > A<double> b;
    > b = a;
    > works, but
    > A<double> c = a; //error
    > doesn't work.


    Isn't this in the FAQ? There is no assignment operator involved
    when you write

    <type-id> name = othername;

    It's called "initialisation" and the actual form is

    <type-id> name(<type-id>(othername));

    (only by form, the syntax immediately above won't work). IOW, you
    attempt to initialise a temporary object of type 'A<double>' from
    the 'a' object, when 'A<double>' has no constructor from 'A<int>'.

    > [...]


    V
    Victor Bazarov, Nov 9, 2005
    #4
  5. Kai-Uwe Bux wrote:
    >> A<int> a;
    >> A<double> b;
    >> b = a;
    >> A<double> c = a; //error

    >
    > Contrary to what the notation suggests, this does not call the
    > assignment operator. Instead a copy constructor is needed: C++
    > considers this line as the construction of the object c from the
    > object a. The above is actually equivalent to:
    >
    > A<double> c ( a );


    Not exactly, but close enough.


    >
    >> }

    >


    V
    Victor Bazarov, Nov 9, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    34
    Views:
    1,491
  2. heng
    Replies:
    8
    Views:
    325
    red floyd
    Dec 5, 2006
  3. Martin T.
    Replies:
    7
    Views:
    808
    Martin T.
    Mar 10, 2008
  4. Wolfnoliir
    Replies:
    7
    Views:
    477
    Francesco
    Aug 30, 2009
  5. Qi
    Replies:
    2
    Views:
    671
Loading...

Share This Page