What is wrong with this simple code!?

I

IWP506

10 points (and a cookie) to whoever tells me why this code generates
these errors:

C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2065: 'cout' : undeclared identifier
C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2297: '<<' : illegal, right operand has type 'char [2]'

#include <iostream.h>

int main()
{
cout << "h";
return 0;
}

Thanks
(e-mail address removed)
 
V

Victor Bazarov

10 points (and a cookie) to whoever tells me why this code generates
these errors:

C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
error C2065: 'cout' : undeclared identifier
C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
error C2297: '<<' : illegal, right operand has type 'char [2]'

#include <iostream.h>

Should be

int main()
{
cout << "h";
return 0;
}

Thanks
(e-mail address removed)

Rename the file to .cpp or .C or .cxx or .c++ or anything that would
make it a C++ source file. Your compiler apparently attempts to treat
it as a C source file.

V
 
B

BigBrian

10 points (and a cookie) to whoever tells me why this code generates
these errors:

C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2065: 'cout' : undeclared identifier
C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2297: '<<' : illegal, right operand has type 'char [2]'

#include <iostream.h>

It should be
#include said:
int main()
{
cout << "h";

std::cout << "h" << std::endl;
 
I

IWP506

I see... so I could using "using namespace std;" then if I wanted to
just make it "cout" and not "std::cout"?

BTW, thanks for the uberly quick responses.
 
J

j

I see... so I could using "using namespace std;" then if I wanted to
just make it "cout" and not "std::cout"?

BTW, thanks for the uberly quick responses.

The namespaces are there for a reason, so your code does not trip-over some
other library code, so it is not a good idea just to discard the namespaces.
Try getting use to writing std:: and use namespaces in your own code too..
with code completion and copy & paste it really is not that hard to get used
to..

Jesper
 
K

Karl Heinz Buchegger

10 points (and a cookie) to whoever tells me why this code generates
these errors:

C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2065: 'cout' : undeclared identifier
C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c(5)
: error C2297: '<<' : illegal, right operand has type 'char [2]'

In addition:

C:\Programming\Microsoft Visual Studio\MyProjects\HelloWorld\main.c
***

If you haven't changed anything else, than VC++ assumes this is C code not C++.
For C++ use a file extension of *.cpp
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top