cout undefined after #include <iostream> ?!

G

Guest

In the code shown below, I thought I was going to puke because the standard prohibits the definition of crii but legalizes cris

But Visual Studio .NET 2003 gave a different reason
C:\Documents and Settings\ndiamond\My Documents\refwhat>cl -GX refwhat.cp
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x8
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved

refwhat.cp
refwhat.cpp(9) : error C2065: 'cout' : 定義ã•ã‚Œã¦ã„ãªã„識別å­ã§ã™ã€

What? There was no error in the #include of <iostream>, the contents of iostream define cout, and the Visual Studio environment variables are all correct. How is it possible for cout to be an undefined identifier, error C2065 in line 9? How is it possible

C++ program
====
#include <iostream

volatile int vi = 2
volatile short vs = 2
// const int &crii = vi
const int &cris = vs

int main()
cout << "I'm going to puke now.\n"
return 0

====

Relevant environment variables

INCLUDE=C:\Program Files\Microsoft Visual Studio .NET 2003\VC7\ATLMFC\INCLUDE;C
\Program Files\Microsoft Visual Studio .NET 2003\VC7\INCLUDE;C:\Program Files\M
crosoft Visual Studio .NET 2003\VC7\PlatformSDK\include\prerelease;C:\Program F
les\Microsoft Visual Studio .NET 2003\VC7\PlatformSDK\include;C:\Program Files\
icrosoft Visual Studio .NET 2003\SDK\v1.1\include;C:\Program Files\Microsoft Vi
ual Studio .NET 2003\SDK\v1.1\include\;C:\Program Files\Microsoft SDK\Include\.
C:\Program Files\Microsoft Visual Studio\VC98\atl\include;C:\Program Files\Micr
soft Visual Studio\VC98\mfc\include;C:\Program Files\Microsoft Visual Studio\VC
8\includ

LIB=C:\Program Files\Microsoft Visual Studio .NET 2003\VC7\ATLMFC\LIB;C:\Progra
Files\Microsoft Visual Studio .NET 2003\VC7\LIB;C:\Program Files\Microsoft Vis
al Studio .NET 2003\VC7\PlatformSDK\lib\prerelease;C:\Program Files\Microsoft V
sual Studio .NET 2003\VC7\PlatformSDK\lib;C:\Program Files\Microsoft Visual Stu
io .NET 2003\SDK\v1.1\lib;C:\Program Files\Microsoft Visual Studio .NET 2003\SD
\v1.1\Lib\;C:\Program Files\Microsoft SDK\Lib\.;C:\Program Files\Microsoft Visu
l Studio\VC98\mfc\lib;C:\Program Files\Microsoft Visual Studio\VC98\li

Path=C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE;C:\Program
iles\Microsoft Visual Studio .NET 2003\VC7\BIN;C:\Program Files\Microsoft Visua
Studio .NET 2003\Common7\Tools;C:\Program Files\Microsoft Visual Studio .NET 2
03\Common7\Tools\bin\prerelease;C:\Program Files\Microsoft Visual Studio .NET 2
03\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.
\bin;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322;C:\WINDOWS\system32;C:\WINDOW
;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Sonic Shared;C:\Progra
Files\Microsoft SDK\Bin\.;C:\Program Files\Microsoft SDK\Bin\WinNT\.;C:\VXIpnp\
inNT\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Progra
Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Vis
al Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;C:\Pro
ram Files\Microsoft SDK\Bin\.;C:\Program Files\Microsoft SDK\Bin\WinNT\.
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WS
 
J

Jonathan Turkanis

Norman Diamond said:
In the code shown below, I thought I was going to puke because the
standard prohibits the definition of crii but legalizes cris.

Always try to post the shortest example that produces the error. For
instance, this would have been sufficient:

#include <iostream>

int main() {
cout << "I'm going to puke now.\n";
return 0;
}

Furthermore, you can eliminate the 'return 0':

#include <iostream>

int main() {
cout << "I'm going to puke now.\n";
}

Finally, we don't care exactly when you're going to puke, so you could
have posted:

#include <iostream>

int main() {
cout << "I'm going to puke\n";
}

But Visual Studio .NET 2003 gave a different reason:
C:\Documents and Settings\ndiamond\My Documents\refwhat>cl -GX refwhat.cpp
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

refwhat.cpp
refwhat.cpp(9) : error C2065: 'cout' : ??????????????

However, the error message says it all -----------^

See also


http://www.softadvances.com/stl/error_c2065_cout_undeclared_identifier.html

Jonathan
 
G

Gary Chang

Hi Norman,

Did you forget to add the namespace directive of "std"?

...
using namespace std;
int main(...)
...

or
std::cout << ...


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
G

Guest

Jonathan Turkanis wrote
Norman Diamond said:
In the code shown below, I thought I was going to puke because th
standard prohibits the definition of crii but legalizes cris

Always try to post the shortest example that produces the error

True. The reason why I forgot to do so was that I'd already constructed a shortest example for a different purpose, which you quoted

Furthermore, you can eliminate the 'return 0'
#include <iostream
int main()
cout << "I'm going to puke now.\n"


Wrong. C++ standard section 6.6.3
But Visual Studio .NET 2003 gave a different reason
C:\Documents and Settings\ndiamond\My Documents\refwhat>cl -GX refwhat.cp
Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x8
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved
refwhat.cpp(9) : error C2065: 'cout' : ?????????????

However, the error message says it all -----------

Which is exactly what I asked about

See als
http://www.softadvances.com/stl/error_c2065_cout_undeclared_identifier.htm

Thank you. I'd like to quote you on "Always try to post the shortest [...]" and say that you could have done it in two lines, but then you wouldn't have learned that if a value-returning (i.e. non-void) function flows off the end then behavior is undefined. Anyway, thank you, your cited page explains it

(Now I wonder why gcc accepted it without even a warning.)
 
G

Guest

Gary Chang wrote

Did you forget to add the namespace directive of "std"
using namespace std
o
std::cout << ..

Thank you. This was my first time to use "cout" in Visual Studio. Now I wonder why I gcc never required a "std" directive, but do understand that Visual Studio requires it

(I had made this as a test case for an unrelated issue, and then copied it to Windows to see if Visual Studio would give the same result. On that other issue, the result was as expected.

Thank you.
 
C

Christoph Rabel

Norman said:
Jonathan Turkanis wrote:

Furthermore, you can eliminate the 'return 0':
#include <iostream>
int main() {
cout << "I'm going to puke now.\n";
}

Wrong. C++ standard section 6.6.3.

No. Look up 3.6.1 para 5

Christoph
 
H

Hendrik Schober

Norman Diamond said:
[...]
Thank you. This was my first time to use "cout" in Visual Studio. Now I wonder why I gcc never required a "std" directive, but
do understand that Visual Studio requires it.

This has to be a bug in GCC. (Which
version are you using?)
[...]
Thank you.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
H

Hendrik Schober

Jonathan Turkanis said:
[...]
#include <iostream>

int main() {
cout << "I'm going to puke now.\n";
return 0;
}

Furthermore, you can eliminate the 'return 0':

#include <iostream>

int main() {
cout << "I'm going to puke now.\n";
}

While you certainly can, it doesn't necessarily
make it a good style to do so. I would never
omit it (consciuously) and I would never recommend
to do this in this newsgroup. I suppose we just
have to agree to disagree about style -- so let's
just say that whether this is recommended for
postings here is open to be debated.
[...]
Jonathan

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
C

Christoph Rabel

Hendrik said:
This has to be a bug in GCC. (Which
version are you using?)

Probably a version < 3.0
This is a known issue with the older versions.

Christoph
 
J

Jonathan Turkanis

Norman Diamond said:
Jonathan Turkanis wrote:
Furthermore, you can eliminate the 'return 0':
#include <iostream>
int main() {
cout << "I'm going to puke now.\n";
}

Wrong. C++ standard section 6.6.3.

Main is a special case; see 3.6.1/5.
Thank you. I'd like to quote you on "Always try to post the
shortest [...]" and say that you could have done it in two lines, but
then you wouldn't have learned that if a value-returning (i.e.
non-void) function flows off the end then behavior is undefined.
Anyway, thank you, your cited page explains it.

Okay, I have to admit my long-winded reply was a bad joke. I thought
it was in poor taste to post an example about puking, and I wasn't
sure the question was entirely serious, so I decided to have some fun.

Whether you like my sense of humor or not, I thought it would be
obvious I was joking when I said you should omit the word 'now'.

Best Regards,
Jonathan
 
J

Jonathan Turkanis

Hendrik Schober said:
While you certainly can, it doesn't necessarily
make it a good style to do so. I would never
omit it (consciuously) and I would never recommend
to do this in this newsgroup. I suppose we just
have to agree to disagree about style -- so let's
just say that whether this is recommended for
postings here is open to be debated.

Yeah, I was really kidding, since the posted example contained a lot
of irrelevant stuff, and I found the comments about 'puking' a bit
inappropriate.

Jonathan
 
H

Hendrik Schober

Jonathan Turkanis said:
[...]
Yeah, I was really kidding, since the posted example contained a lot
of irrelevant stuff, and I found the comments about 'puking' a bit
inappropriate.

Sorry, beeing a non-native this excaped
me. :blush:>

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
J

Jonathan Turkanis

Hendrik Schober said:
Jonathan Turkanis said:
[...]
Yeah, I was really kidding, since the posted example contained a lot
of irrelevant stuff, and I found the comments about 'puking' a bit
inappropriate.

Sorry, beeing a non-native this excaped
me. :blush:>

It's okay -- I should learn not to post in the middle of the night ;-)

Jonathan
 
G

Guest

Jonathan Turkanis wrote
Norman Diamond said:
Jonathan Turkanis wrote
Furthermore, you can eliminate the 'return 0'

Main is a special case; see 3.6.1/5

You're right. I'm going to puke again (because of the standard, not because of you)

Okay, I have to admit my long-winded reply was a bad joke

You're right

Whether you like my sense of humor or not, I thought it would b
obvious I was joking when I said you should omit the word 'now'

Yes, that part was obviously a joke. Now I'd better add a belated smiley to my second "you're right", since the humorous intent isn't always obvious even if we do have similar senses of humor.
 
J

Jonathan Turkanis

Norman Diamond said:
Jonathan Turkanis wrote:
"Norman Diamond" <[email protected]> wrote in
Okay, I have to admit my long-winded reply was a bad joke.

You're right.

Whether you like my sense of humor or not, I thought it would be
obvious I was joking when I said you should omit the word 'now'.

Yes, that part was obviously a joke. Now I'd better add a belated
smiley to my second "you're right", since the humorous intent isn't
always obvious even if we do have similar senses of humor.

;-)

Jonathan
 

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

Top