C++ experts.. come in and explain something (simple) for me!

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
I was trying out Dev-C++ and tried to compile a few simple lines:

---
#include <iostream>
#include <string>

int main()
{
string x = "Hello world!";
cout << x <<endl;
system("PAUSE");
return 0;
}
---

Without "using namespace std", the program cannot compile as if I did not have #include <iostream> or #include <string>.. why is this so? Before this many of my C++ programs can compile fine using GCC on a Solaris system without defining namespace.. :confused:

And here's a newbie question: what do I need <cstdlib> for? :eek: It's been a long while since I last used C++.. I'm guessing this is for some C functions?

:)
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
SLEEP is located in cstdlib. You shouldn't have any namespace issues if you include that.

-silver
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Before this many of my C++ programs can compile fine using GCC on a Solaris system without defining namespace.

Did you look at the version of gcc on Solaris and the one you're running locally?

system("PAUSE");

You don't really want to do that.
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
cout is part of std namespace so you either need to add "using namespace std" or fully qualify names like: "std::cout <<"..."

On gcc cout is defined in the global namespace.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: agnitrate
SLEEP is located in cstdlib. You shouldn't have any namespace issues if you include that.

-silver
I don't understand what you are saying in your second sentence :confused:


Originally posted by: Nothinman
Before this many of my C++ programs can compile fine using GCC on a Solaris system without defining namespace.

Did you look at the version of gcc on Solaris and the one you're running locally?

system("PAUSE");

You don't really want to do that.

Ahhhh.. now it doesn't work without namespace using g++ in the Solaris system :confused: I swear it worked a year ago (I saved all my previous code and I didn't use namespace :confused: )!!! :p

What's wrong with system("PAUSE")?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I swear it worked a year ago

It probably did, the C++ standard has changed a bit since it was originally introduced and gcc has gotten more strict about it.

What's wrong with system("PAUSE")?

Other than the fact that it uses system to call a shell command, it would only work on Windows because no shell on unix that I know of supports the "pause" command. You could be better off using sleep to have it pause/sleep for a set amount of time before exiting or use a standard call like cin to perform the same function as the pause.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: Nothinman
I swear it worked a year ago

It probably did, the C++ standard has changed a bit since it was originally introduced and gcc has gotten more strict about it.

What's wrong with system("PAUSE")?

Other than the fact that it uses system to call a shell command, it would only work on Windows because no shell on unix that I know of supports the "pause" command. You could be better off using sleep to have it pause/sleep for a set amount of time before exiting or use a standard call like cin to perform the same function as the pause.

Cool, good to know what system() actually does.

I'll just use cin.get() from now on... unless there's something wrong with that too..

Thanks! :)
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Your problem was that you have two options:

#include <iostream>
using namespace std;

OR....

#include <iostream.h>

You have to specify iostream.h if you're not using namespace std.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: CTho9305
Your problem was that you have two options:

#include <iostream>
using namespace std;

OR....

#include <iostream.h>

You have to specify iostream.h if you're not using namespace std.

It doesn't compile (same error messages) if I omit using namespace std and add the .h's :confused:
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Even if it did work, it would only be for backwards compatibility with older code so you should avoid it.
 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
Originally posted by: BingBongWongFooey
Worry less about "what works" and worry more about "what is correct."

That is exactly what i'm concerned about, setting up a few "conventions" of doing things the right way :)
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: screw3d
Originally posted by: BingBongWongFooey
Worry less about "what works" and worry more about "what is correct."

That is exactly what i'm concerned about, setting up a few "conventions" of doing things the right way :)

Cool. :) Yeah as far as standard C++ headers go, none have a ".h", that's the old non-standard way.