By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

problem in converting & to *

P: n/a
Hi, I'm tryng to convert for study purpose the following code but I
have an error:

FROM
friend ostream &operator<<( ostream &, const Array & );

TO
friend ostream *operator<<( ostream *, const Array * );

ERROR:
'std::ostream* operator<<(std::ostream*, const Array*)' must have an
argument of class or enumerated

why?

Thanks

Feb 20 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
josh wrote:
Hi, I'm tryng to convert for study purpose the following code but I
have an error:

FROM
friend ostream &operator<<( ostream &, const Array & );

TO
friend ostream *operator<<( ostream *, const Array * );

ERROR:
'std::ostream* operator<<(std::ostream*, const Array*)' must have an
argument of class or enumerated

why?

Thanks
You cannot overload operators for pointer types. Both the arguments for
your operator<< are pointers.

For any overloaded operator at least one of the argument must be a
class, struct or enumeration (or a reference to same).

john
Feb 20 '07 #2

P: n/a
John Harrison wrote:
josh wrote:
>Hi, I'm tryng to convert for study purpose the following code but I
have an error:

FROM
friend ostream &operator<<( ostream &, const Array & );

TO
friend ostream *operator<<( ostream *, const Array * );

ERROR:
'std::ostream* operator<<(std::ostream*, const Array*)' must have an
argument of class or enumerated

why?

Thanks

You cannot overload operators for pointer types. Both the arguments for
your operator<< are pointers.

For any overloaded operator at least one of the argument must be a
class, struct or enumeration (or a reference to same).

john
What is your 'study purpose'?

john
Feb 20 '07 #3

P: n/a
* josh:
Hi, I'm tryng to convert for study purpose the following code but I
have an error:

FROM
friend ostream &operator<<( ostream &, const Array & );

TO
friend ostream *operator<<( ostream *, const Array * );

ERROR:
'std::ostream* operator<<(std::ostream*, const Array*)' must have an
argument of class or enumerated

why?
Operators can only be overloaded for class or enum type arguments.

That way there is a little bit of the core language you can rely on no
matter what fancy definitions SomeOne(TM) has provided.

It also, unfortunately, means that the unsuspecting Java programmer who
writes

int const x = 42;
string const s = "The answer is " + x;

is in for a surprise.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 20 '07 #4

P: n/a
On 20 Feb, 09:22, John Harrison <john_androni...@hotmail.comwrote:
John Harrison wrote:
josh wrote:
Hi, I'm tryng to convert for study purpose the following code but I
have an error:
FROM
friend ostream &operator<<( ostream &, const Array & );
TO
friend ostream *operator<<( ostream *, const Array * );
ERROR:
'std::ostream* operator<<(std::ostream*, const Array*)' must have an
argument of class or enumerated
why?
Thanks
You cannot overload operators for pointer types. Both the arguments for
your operator<< are pointers.
For any overloaded operator at least one of the argument must be a
class, struct or enumeration (or a reference to same).
john

What is your 'study purpose'?

john
Oh I want say that when I meet in code exercises references types I
try to rewrite it to pointer notations to see how to make that

Feb 20 '07 #5

P: n/a
It also, unfortunately, means that the unsuspecting Java programmer who
writes

int const x = 42;
string const s = "The answer is " + x;

is in for a surprise.
sorry I don't understand well what you want say!

Feb 20 '07 #6

P: n/a
josh <xd********@yahoo.comwrote:
>It also, unfortunately, means that the unsuspecting Java programmer who
writes

int const x = 42;
string const s = "The answer is " + x;

is in for a surprise.

sorry I don't understand well what you want say!
Since "The answer is " is of type const char*, which is a built-in
pointer type, and x is of type const int (another built-in type), then
the operator+ cannot be overloaded for this case. Therefore, the
initialization of s will have the effect of taking the address of the
beginning of "The answer is " and adding 42 to it, instead of
concatenating the string-ized version of x, which is most likely not
what was desired.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 20 '07 #7

P: n/a
Marcus Kwok wrote:
josh <xd********@yahoo.comwrote:
>>It also, unfortunately, means that the unsuspecting Java programmer who
writes

int const x = 42;
string const s = "The answer is " + x;

is in for a surprise.
sorry I don't understand well what you want say!

Since "The answer is " is of type const char*,
Nope it's type const char[15].
which is a built-in pointer type,
which is an array of 15 char (built in).
and x is of type const int (another built-in type), then
the operator+ cannot be overloaded for this case. Therefore, the
initialization of s will have the effect of taking the address of the
beginning of "The answer is " and adding 42 to it, instead of
concatenating the string-ized version of x, which is most likely not
what was desired.

Correct. Further, while there is a conversion from const char* to
string, there isn't one for int, so:

string s = string("The answer is ") + string(x);

wouldn't work either.

If you want to do fancy formatting you have to use a stream:

ostringstream os ;
os << "The answer is " << x ;
const string s = os.str();

Feb 22 '07 #8

P: n/a
Ron Natalie <ro*@spamcop.netwrote:
Marcus Kwok wrote:
>Since "The answer is " is of type const char*,

Nope it's type const char[15].
>which is a built-in pointer type,

which is an array of 15 char (built in).
Right, thanks for the correction.
If you want to do fancy formatting you have to use a stream:

ostringstream os ;
os << "The answer is " << x ;
const string s = os.str();
Yes, though I use the technique in the FAQ of wrapping this in a
templated function, so I can use it like:

string s = string("The answer is ") + stringify(x);

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 22 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.