cout not defined !?!

C

cppaddict

I'm getting an compile error (using cygwin gcc on windows xp) when I
try to compile the following simple program:

#include <iostream>

int main(void) {
cout << "C++ Hello World";
return 0;
}

It says that cout is undefined. Adding the line "using namespace
std;" after the include doesn't fix the problem. Anyone know what's
going on and how I can fix it?

Thanks,
cpp
 
J

joe martin

I'm getting an compile error (using cygwin gcc on windows xp) when I
try to compile the following simple program:

#include <iostream>

int main(void) {
cout << "C++ Hello World";
return 0;
}

It says that cout is undefined. Adding the line "using namespace
std;" after the include doesn't fix the problem. Anyone know what's
going on and how I can fix it?

Thanks,
cpp

I believe you need the good old std namespace. Try:

std::cout << "C++ Hello World" << std::endl;

or putting "using namespace std;" under your #include
 
C

cppaddict

I believe you need the good old std namespace. Try:

std::cout << "C++ Hello World" << std::endl;

or putting "using namespace std;" under your #include

If you take a look at the original post, I say that I've already tried
that, to no avail...

The error message in that case is: "Undefined reference to
std::cout...."

Any other ideas?

Thanks,
cpp
 
J

joe martin

If you take a look at the original post, I say that I've already tried
that, to no avail...

The error message in that case is: "Undefined reference to
std::cout...."

Any other ideas?

Thanks,
cpp

Ahh sorry, you have to use the std namespace and use g++ not gcc. I
have your same environment and it works for me.

-joe
 
R

Russell Hanneken

cppaddict said:
I'm getting an compile error (using cygwin gcc on windows xp) when I
try to compile the following simple program:

#include <iostream>

int main(void) {
cout << "C++ Hello World";
return 0;
}

It says that cout is undefined. Adding the line "using namespace
std;" after the include doesn't fix the problem. Anyone know what's
going on and how I can fix it?

cout is in the std namespace, so you do need to indicate the namespace
("using namespace std;" is one way).

Other than that, there's nothing wrong with the code (except technically
you should also #include <ostream>, though with most compilers it isn't
necessary). My guess is that you're getting a linker error because you
built the program like this:

gcc hello.cpp

instead of like this:

g++ hello.cpp

BTW, I would also recommend you use the flags "-ansi -pedantic -W
-Wall," if you aren't already.
 

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
474,166
Messages
2,570,902
Members
47,442
Latest member
KevinLocki

Latest Threads

Top