'no user-defined conversion exists' error

Discussion in 'C++' started by Anna, Jan 9, 2007.

  1. Anna

    Anna Guest

    I have two data types in my program:

    Foo1 and
    FooTD (which is a typedef of type Foo2)

    Foo1 has an operator= function which takes a const& to Foo2 as input
    and assigns it to a Foo1 type.

    The statement

    Foo1 f = getFooTD();

    where getFooTD() returns a Foo2 doesn't compile. I get the 'no
    user-defined conversion exists' error.

    However, the following works:

    Foo1 f;
    f = getFooTD();

    There's probably something very obvious that I'm missing here. Anyone
    knows why I get the error?

    Thanks.
     
    Anna, Jan 9, 2007
    #1
    1. Advertising

  2. Anna

    red floyd Guest

    Anna wrote:
    > I have two data types in my program:
    >
    > Foo1 and
    > FooTD (which is a typedef of type Foo2)
    >
    > Foo1 has an operator= function which takes a const& to Foo2 as input
    > and assigns it to a Foo1 type.
    >
    > The statement
    >
    > Foo1 f = getFooTD();
    >
    > where getFooTD() returns a Foo2 doesn't compile. I get the 'no
    > user-defined conversion exists' error.
    >
    > However, the following works:
    >
    > Foo1 f;
    > f = getFooTD();
    >
    > There's probably something very obvious that I'm missing here. Anyone
    > knows why I get the error?
    >



    Yes. Foo1 f = getFooTD() is construction, not assignment. It's
    semantically equivalent to:

    Foo1 f(getFooTD()).

    You need a constructor for Foo1 that takes a Foo2.
     
    red floyd, Jan 9, 2007
    #2
    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. Totan
    Replies:
    0
    Views:
    1,007
    Totan
    Apr 17, 2006
  2. Replies:
    0
    Views:
    379
  3. Oodini
    Replies:
    1
    Views:
    1,788
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    1
    Views:
    509
    Sion Arrowsmith
    Jul 10, 2008
  5. Ulf Meinhardt
    Replies:
    8
    Views:
    6,168
Loading...

Share This Page