'no user-defined conversion exists' error

A

Anna

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

red floyd

Anna said:
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.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top