473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

cout2,

Why does the first statement output nonsense, but not the second. That
is, why aren't the statements equivelement?

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);
cout << "sin(2)=" << sin(2) << endl;
}

Nov 7 '06 #1
1 1298
vsgdp wrote:
Why does the first statement output nonsense, but not the second. That
is, why aren't the statements equivelement?

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
cout.operator <<("sin(2)=").operator <<(sin(2)).operator <<(endl);
cout.operator << "sin(2)="; invokes the member function
ostream::operator<< (const void* param)
and not what you expected:
inline ostream operator<< (ostream& _O, const char *param)
The reason for this is that you explicitely ask for the member function
operator<<, but operator<< for char* is defined as static function (as
most other custom defined stream operators are). What you are seeing is
the address of the string.
cout << "sin(2)=" << sin(2) << endl;
}
You should always stick to this invokation scheme.

Regards,
Stuart
Nov 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard | last post by:
I got 2 error in the program code below, but I don't know what they are and how to fix it. Can anyone put it in compile and tell me what is wrong? I am new to c++ #include <iostream> #include...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.