cin,cout,and endl of c++

Aug 26, 2010 at 2:58am
Hi,
Dear everybody,I write a program,it includes followed head files:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <math.h>

But,when I compile it,screen out:
error: `cout' was not declared in this scope
error: `endl' was not declared in this scope
error: `cin' was not declared in this scope
Could please youtell me how can I do,thank you.
wenli wang
th26,August,2010




Aug 26, 2010 at 4:40am
Add:
1
2
#include <iostream>
using namespace std;
Aug 26, 2010 at 5:45pm
In addition, <fstream.h> is deprecated. Use <fstream> instead. You should also use <cstring>, <cstdio>, <cstdlib> and <cmath> instead of the C (.h) variants.

I recommend that you avoid "using namespace std;" if you can avoid it. Prefer to use "std::cin", "std::cout" and "std::endl" instead. It's a bad habit that many programmers learn in school that they struggle to break as a professional programmer.
Aug 26, 2010 at 7:20pm
If I can ask, why is that a bad habit?
Aug 26, 2010 at 7:25pm
It's a bad habit because it's incorrect. All C++ standard headers (including those from the C library) have no .h suffix. I would assume this is to separate standard headers from user-created/non-standard ones (e.g. the Windows and POSIX APIs)
Aug 26, 2010 at 7:47pm
Ok, but I mean the namespace thing :p
Aug 26, 2010 at 7:48pm
Oh; there are several reasons for that. One of them has to do with name conflicts.

I'd break that habit ASAP, I did; it really isn't difficult to prefix "std::" to things.
Aug 26, 2010 at 7:52pm
ok..I'll have a look on google to find why :)
Aug 26, 2010 at 8:00pm
Using namespaces globally is not a bad habit, imo. When creating applications that only use the STL, the C library and standard C++ no naming conflicts should emerge. I do, however agree that you should not make a habit of it. Be wary of the effect of (using) namespaces, so that when things go (horribly) wrong you know what to do.
Aug 26, 2010 at 8:02pm
Is it worth the risk? It probably takes more energy to "Be wary of the effect" than it does to type "std::" a few times.
Aug 26, 2010 at 11:26pm
Namespaces separate and organize functionality. You can have a "xander333::sort()" function and it won't conflict with "std::sort()" or "boost::sort()" or any other sort(). Without namespaces their can be only one sort().

Now let's say you've put "using namespace std;" in all your source files and you've implemented a simple templated function called "fill()" in the global namespace of one of your files. This file also depends on a header from libFoo -- "foo.hpp". Version 2.1 of libFoo comes out and all of a sudden your program no longer compiles. You version of "fill()" suddenly conflicts with another "fill()"! What happened???

It turns out that the folks implementing libFoo included <algorithm> in the new version of "foo.hpp" when they didn't before. Now you have all of the standard algorithms being included in your source file, and your "using namespace std;" has pulled them all into the global namespace. std::fill() now directly conflicts with your fill().

More insidious, you've gotten your code to compile by renaming your fill() to xander333_fill(), but something's not working right -- your report numbers are off. It turns out that your custom divides() function, which does fixed precision math, is no longer being called because the templated function from <functional> (also newly included by "foo.hpp") makes for a better match because you're calling types did not exactly match the declared types.
Aug 27, 2010 at 1:34am
I try to compile it again using methods given by Kyon and PanGalactic,but fail.I think may be the LD_LIBRARY_PATH environment variables is wrong and computer can not find the head files.I do this in redhat linux enterprise4.8(gnu gcc 3.2.3 or latter)
g++ plot_fitsfile.cc -I/usr/include
But out again:
error: `cout' was not declared in this scope
error: `endl' was not declared in this scope
error: `cin' was not declared in this scope
What should I do?Thank you.
Last edited on Aug 27, 2010 at 1:39am
Aug 27, 2010 at 3:31am
Use std::cout, std::cin, and std::endl.
Aug 27, 2010 at 6:12am
chrisname wrote:
Is it worth the risk? It probably takes more energy to "Be wary of the effect" than it does to type "std::" a few times.
By "Be wary of the effect" I mean, know what namespaces actually do. Which is slight renaming. Either stating using "using namespace std;" or use "std::.." only are wrong. I personally don't like using things I don't know about.

@Wengli Wang: What's the code you are trying to compile, in it's full extends?
Aug 28, 2010 at 8:58am
you will, in time come across occasions when you get name clashes.
then you will understand, grasshopper.

it's OK for trivial stuff I'd say
Aug 31, 2010 at 1:06am
Mr Kyon,The length of code is bigger than max length and can't post here.Could please you send your email address to china.wenli.wang@gmail.com.I will send the codes to you,and please you give me some advice for compile it.Thank you.
Sep 4, 2010 at 6:16am
mr kyon thanks U so much cause I'l be learn c++ brginner on linux........
Topic archived. No new replies allowed.