On Nov 20, 2:23 pm, red floyd <(E-Mail Removed)> wrote:

> (E-Mail Removed) wrote:

> > I have read C++ Primer 3rd in 2001 and I am reading the 4th edtion of

> > this book now. I find a bug in the book, chapter 14.9, subtitle is

> > "Ambiguities When Two Classes Define Conversions". The source code

> > captured from the book is as follows:

>

> > ///////////////////////////////////////////////////////////////////////////-///////////////

> > class Integral;

> > class SmallInt {

> > public:

> > SmallInt(Integral); // convert from Integral to SmallInt

> > // ...

> > };

> > class Integral {

> > public:

> > operator SmallInt() const; // convert from SmallInt to

> > Integral

>

> You've misread this. This isn't a constructor creating an Integral from

> a SmallInt, it's a conversion operator changing an Integral to a SmallInt.

>

> > // ...

> > };

> > void compute(SmallInt);

> > Integral int_val;

> > compute(int_val); // error: ambiguous

> > ///////////////////////////////////////////////////////////////////////////-///////////////

>

> > The g++ and VC2005(express) will both raise compiler errors for this

> > section of source code. I think the compiler are correct for:

> > SmallInt(Integral) use Integral, so this method should see the

> > Integral definition, not declaration.

>

> So what makes the call ambiguous is, does the compiler use the

> SmallInt::SmallInt(Integral) constructor, or the Intergral:perator

> SmallInt conversion operator?- Hide quoted text -

>

> - Show quoted text -
I got what you said. You misread what I said.

class Integral;

class SmallInt {

public:

SmallInt(Integral); // convert from Integral to SmallInt //

It need see the Integral definition

// ...

};

class Integral {

public:

operator SmallInt() const; // convert from SmallInt to, it

need see the SmallInt definition

Integral

// ...

};

void compute(SmallInt);

Integral int_val;

compute(int_val); // error: ambiguous

How could we write the code? A need see the B defintion to complete

its defintion while B need know A definition to finish its

definition.