'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. Advertisements

  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. Advertisements

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. Replies:
    0
    Views:
    453
  2. Oodini
    Replies:
    1
    Views:
    2,083
    Keith Thompson
    Sep 27, 2005
  3. Kavya
    Replies:
    2
    Views:
    791
    Kavya
    Oct 31, 2006
  4. Replies:
    1
    Views:
    565
    Sylvester Hesp
    May 16, 2007
  5. hurcan solter
    Replies:
    3
    Views:
    1,027
    Cholo Lennon
    Aug 29, 2007
  6. Belebele
    Replies:
    6
    Views:
    597
    Belebele
    Dec 11, 2007
  7. Replies:
    1
    Views:
    695
    Sion Arrowsmith
    Jul 10, 2008
  8. Arne Mertz
    Replies:
    12
    Views:
    881
    Arne Mertz
    Apr 13, 2009
Loading...