A bug in <C++ Primer 4th>.

Discussion in 'C++' started by ankyhe@gmail.com, Nov 20, 2007.

  1. Guest

    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
    // ...
    };
    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.

    I feel sorry I can't find the error list of this book. Could anybody
    can tell me where to find the error list of <C++ Primer 4th> or give
    me one of the authors email address? Thanks a lot.
    , Nov 20, 2007
    #1
    1. Advertising

  2. red floyd Guest

    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::eek:perator
    SmallInt conversion operator?
    red floyd, Nov 20, 2007
    #2
    1. Advertising

  3. Guest

    On Nov 20, 2:23 pm, red floyd <> wrote:
    > 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::eek: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.
    , Nov 20, 2007
    #3
  4. Jim Langston Guest

    <> wrote in message
    news:...
    > On Nov 20, 2:23 pm, red floyd <> wrote:
    >> 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::eek: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.


    That's what header files are for. Or rearranging code if you don't do
    headers.

    class Integral;
    class SmallInt {
    public:
    SmallInt(Intergral);
    };

    class Integral {
    public:
    operator SmallInt() const;
    }

    SmallInt::SmallInt(Integral) {
    // We now know Integral so can use it
    }

    Normally the first part (declaration) would be in a header file, the bottom
    part (definition) would be in a .cpp file but that is not always strictly
    required and on small programs may be okay not to.
    Jim Langston, Nov 20, 2007
    #4
  5. Jim Langston Guest

    Bah, ignore what I said, it's not working. Experimenting

    "Jim Langston" <> wrote in message
    news:nmw0j.440$...
    [snip OP question]
    >
    > That's what header files are for. Or rearranging code if you don't do
    > headers.
    >
    > class Integral;
    > class SmallInt {
    > public:
    > SmallInt(Intergral);
    > };
    >
    > class Integral {
    > public:
    > operator SmallInt() const;
    > }
    >
    > SmallInt::SmallInt(Integral) {
    > // We now know Integral so can use it
    > }
    >
    > Normally the first part (declaration) would be in a header file, the
    > bottom part (definition) would be in a .cpp file but that is not always
    > strictly required and on small programs may be okay not to.


    This answer is wrong, doesn't work.
    Jim Langston, Nov 20, 2007
    #5
  6. 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


    The line above _declares_ a ctor. The compiler knows, that Integral is a
    type. That's enough here.

    > SmallInt(Integral) use Integral, so this method should see the
    > Integral definition, not declaration.


    Nope; the definition is only required, if you
    - instantiate an Integral object,
    - use sizeof(Integral),
    - access an Integral::member,
    - (more?)

    > void compute(SmallInt);
    > Integral int_val;
    > compute(int_val); // error: ambiguous


    you cannot call a function in global scope. I'd write

    Integral i;
    SmallInt s = i; // error: ambiguous

    but I'm not sure, whether you've ommitted some lines in your quote.

    Ralph
    Ralph D. Ungermann, Nov 20, 2007
    #6
    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. Andrew Koenig

    Announcement: C++ Primer, 4th edition

    Andrew Koenig, Jan 17, 2005, in forum: C++
    Replies:
    9
    Views:
    437
    red floyd
    Jan 19, 2005
  2. hajime

    C++ Primer, 4th Edition

    hajime, Aug 16, 2005, in forum: C++
    Replies:
    5
    Views:
    457
    hajime
    Aug 16, 2005
  3. asdf
    Replies:
    6
    Views:
    287
    Default User
    Sep 18, 2006
  4. asdf
    Replies:
    3
    Views:
    249
    KiLVaiDeN
    Oct 11, 2006
  5. arnuld
    Replies:
    17
    Views:
    594
    Marcus Kwok
    Oct 17, 2006
Loading...

Share This Page