imported libraries and namespaces

Hello, I have a simple question. When I comment the line of code "#include <iostream>" and run the program it runs without any errors, but when uncomment "#include <iostream>" and then comment "using namespace std;" I get the following error "main.cpp:19:5: error: ‘cout’ was not declared in this scope". My question is isn't cin and cout part of the iostream library? and if they are how come I can't use them without using "using namespace std;"? Another thing, I just discovered that I am able to make a user defined class part of an already defined namespace (ex. namespace std { class myClass; } ), could you please tell me what is the point in doing something like that, while I could've created my own user defined namespace? Thank you for your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <cstdlib>
#include <iostream>
#include <iostream>

//using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    cout << "Hello World" << endl;
    
    return 0;
}
closed account (2b5z8vqX)
My question is isn't cin and cout part of the iostream library?

Yes.

And if they are how come I can't use them without using "using namespace std;"?

The purpose of a using directive is to allow the programmer to reference the entities within the specified namespace without explicit qualification in the enclosing namespace.

When the using directive is removed from your source code, the unqualified names "cout" and "endl" (the ones that are from the std namespace) are not found by the compiler. You can fix this error by qualifying the names referenced from the standard (std) namespace or by uncommeting the using directive.
closed account (o3hC5Di1)
Hi there,

There is a difference between a library and a namespace.

When you include the iostream header file, you make sure that the compiler knows about all the classes and functions you could be using in your program, such as std::cout and std::cin.

Namespaces are used to keep all the names of classes, functions and variables separate, which allows you to organize your code in a tidy way. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace ascii_string {

void replace();
void substring();

}


namespace utf8_string {

void replace();
void substring();

}

//We now have 2x replace and 2x substring, but we can keep them apart as such:

ascii_string::replace();
ascii_string::substring();

utf8_string::replace();
utf8_string::substring();


So the namespace "std" is a namespace for everything in the standard library. This makes sure that if, for example, you define your own function and you would call it, for instance "cout", the compiler still knows which "cout" to use. Note that for this purpose, it is generally recommended not to do using namespace std; for an entire program, only at a local function scope and only if you really need to.

So, if you don't do using namespace std;, you need to type std::cout to let the compiler know that it can find the name "cout" in the "std" namespace.

Hope that makes sense, more info on namespaces is available here: http://www.cplusplus.com/doc/tutorial/namespaces/


All the best,
NwN
Last edited on
Topic archived. No new replies allowed.