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

A

ankyhe

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

red floyd

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?
 
A

ankyhe

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.



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

Jim Langston

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

Jim Langston

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

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

Ralph D. Ungermann

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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top