Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A bug in <C++ Primer 4th>.

Reply
Thread Tools

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

 
 
ankyhe@gmail.com
Guest
Posts: n/a
 
      11-20-2007
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.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      11-20-2007
http://www.velocityreviews.com/forums/(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?
 
Reply With Quote
 
 
 
 
ankyhe@gmail.com
Guest
Posts: n/a
 
      11-20-2007
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.

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      11-20-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


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.


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      11-20-2007
Bah, ignore what I said, it's not working. Experimenting

"Jim Langston" <(E-Mail Removed)> wrote in message
news:nmw0j.440$(E-Mail Removed)...
[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.


 
Reply With Quote
 
Ralph D. Ungermann
Guest
Posts: n/a
 
      11-20-2007
(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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
HTPC Primer, Part I - Video and Audio Silverstrand Front Page News 0 10-21-2005 01:28 PM
PC TV Tuner Primer Silverstrand Front Page News 0 10-19-2005 01:47 PM
Primer needed: Java & Windows Security Chris Java 0 04-18-2005 09:08 PM
SSL / authentication primer Richard Java 2 07-25-2003 02:08 PM



Advertisments