Getting error in cout Simple code - c++

I'm using Visual Studio 2010, and I'm wondering why I'm getting an error.
The error is: cout is undefined
#include<iostream>
#include<stdio.h>
#include<conio.h>
int main()
{
cout<<"Why am I not working ??";
printf("My Name is Khan and I'm not a terrorist.");
return 0;
}

cout is in the std namespace. You either need to declare that you are using the std namespace by adding the following to your code (it is generally put just after includes), though this is generally considered bad practise for non-trivial code:
using namespace std;
Or you can qualify cout every time it is used (this is generally preferred):
std::cout << "Hello, World!" << std::endl;

cout is a global object that lives in the std namespace. You have to fully qualify the name:
std::cout << "Hello";
// ^^^^^
If you really want to omit the qualification, you could have a using declaration in main() before you use the unqualified name cout (in general, avoid putting using declarations at global namespace scope):
// ...
int main()
{
using std::cout;
// ^^^^^^^^^^^^^^^^
cout << "Why I'm not working ??";
// ...
}

Add the following before int main:
using namespace std;

Related

Using namespace scope

I tried the following piece of code. When i compile, i get error that there there are ambiguous instances of first_var, whereas i have introduced
using namespace second_space before the last cout
I guess this is because the last cout is using both the namespaces. There is no override concept for namespaces ? Is there anyway a namespace scope can be ended or it continues from the using namespace point to the end of file?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
using namespace first_space;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Edit 1:
I tried something like this below. Declared a variable with same name inside the main, assigned a value 1 to it and then used using namespace below that. But I see that, the value of first_var is printed as 1 in the last two cout.
There is no ambiguity here. So the namespace didn't have any effect? Why is it so?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
int first_var =1 ;
using namespace first_space;
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
// using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Output:
Hello World
First Namespace Variable using namespace identifier:0
First Namespace Variable using using identifier:1
Second Namespace Variable using using identifier:1
Yes you are correct, after the 2nd using statement, the variable first_var is now ambiguous because both of the two namespaces are valid and of equal precedence as far as name lookup is concerned.
The two workarounds are
a) add braces to enforce an anonymous scope (live demo)
{
using namespace first_space;
cout << "First Namespace Variable using using identifier:" << first_var << endl;
}
{
using namespace second_space;
cout << "Second Namespace Variable using using identifier:" << first_var << endl;
}
b) drop the using keyword and use the namespace scope directly
cout << "First Namespace Variable using using identifier:" << first_space::first_var << endl;
cout << "Second Namespace Variable using using identifier:" << second_space::first_var << endl;
Personally I would go with option b. One of the main reasons you add namespaces in the first place is to avoid ambiguity issues, so polluting the current scope with a bunch of using statements undermines that.

About std:cout in C++

Is there an error in this code:
#include <iostream>
using namespace std;
int main()
{
std:cout << "hello" "\n";
}
GCC detects no error but std:cout does not seem standard.
There's no error. I could rewrite your code to make it clearer:
#include <iostream>
using namespace std;
int main()
{
std:
cout << "hello" "\n";
}
You created a label named std. cout is used unqualified, which is okay since you have the using-directive for std above it. And you can concatenate string literals by writing them next to each other as you did. This is perfectly well-formed code that prints "hello" followed by a newline.
You're defining a label std and then you're calling cout. This is legal because you have using namespace std;
The code has an issue.
while trying to instruct the compiler to use the namespace std, we are trying to call the function cout which is defined in the scope of std.
thus the correct use of the scope resolution operator is
'std::cout '
and not
std:cout.
And others have pointed out
by writing
std:
what you do is create a label.

Is equal() included by default in the global namespace?

This is a question regarding the default global namespace in C++. I have the following code that compiles and runs properly using g++ clang-500.2.79.
#include <string>
#include <iostream>
using std::string;
using std::endl;
using std::cout;
bool is_palindrome(const string& str){
return equal(str.begin(), str.end(), str.rbegin());
}
int main(){
cout << "Hello is a palindrome: " << is_palindrome("Hello") << endl;
cout << "madam is a palindrome: " << is_palindrome("madam") << endl;
return 0;
}
My questions is, why does this code compile properly? I forgot to put #include <algorithm> and using std::equal at the beginning of my file. So the expected behaviour is for the compiler to complain.
The example at http://en.cppreference.com/w/cpp/algorithm/equal confirms that I should be using std::equal.
To investigate this further, I tried to track down exactly which version of the equal() function was being called. Being a relative newbie to C++ I don't know exactly how to do this either. I tried,
cout << "The function is: " << equal << endl;
Which generated a compiler error with some interesting information:
/usr/include/c++/4.2.1/bits/stl_algobase.h:771:5:
note: 'std::equal' declared here
Try as I might, I can't find information about stl_algobase (or more probably, I most likely don't understand what I've found). Is stl_algobase a set of functions that are automatically included in the global namespace?
A further questions is: What is the proper way to track (code or otherwise) down which function is being called when you are dealing with potentially overloaded or template functions in C++?
equal is in the std namespace. What you are seeing is argument dependent lookup (ADL). Because the arguments are in the std, the name lookup for equal considers that namespace too.
Here's a simplified example:
namespace foo
{
struct Bar {};
}
namespace foo
{
void bar(const Bar&) {}
void bar(int) {}
}
int main()
{
foo::Bar b;
foo::bar(b); // OK
bar(b); // ADL, OK
foo::bar(42); // OK
bar(42); // No ADL: error: 'bar' was not declared in this scope
}

Namespace using declaration (C++ Primer - Stanley Lipmann)

Anyone can help me to understand this statement found in chapter 3 (Library Types) by Stanley Lipmann?
"Using an unqualified version of a namespace name without a using declaration is an error, although some compilers may fail to detect this error"
I'm having such hard time understanding the semantics of his sentence (english).
Is he trying to say something like the below scenario?
int main() {
xx::yy
}
where xx is a namespace not defined using the "using" statement and yy is a member?
Example:
cout is a name of the std namespace. The unqualified name is cout. The qualified name is std::cout. It is an error to use the unqualified name(cout) without a using declaration beforehand. You can use either one of the two following declarations:
// This brings in the entire std namespace
using namespace std;
OR
// This only brings in cout. You would still need to qualify other names,
// such as cin, endl, etc...
using std::cout;
What he's saying is that the following code should not compile:
#include <iostream>
void foo() {
cout << "This is an error!" << endl;
}
The cout and endl names are not defined right now. They're declared as std::cout and std::endl, and in order to use them, you can do one of a few things:
#include <iostream>
void foo() {
std::cout << "This, I think, is the best way to do it." << std::endl;
}
Using the fully qualified name prevents collisions later on: you'll never have something else called std::cout.
#include <iostream>
void foo() {
using std::cout;
using std::endl;
cout << "This is pretty good." << endl;
}
Having the using statements specify the exact names you're using, and having the using statements in the function, can save some typing and makes collisions pretty unlikely.
#include <iostream>
using namespace std;
void foo() {
cout << "This works, but isn't good." << endl;
}
Importing the entire std namespace makes it pretty likely that you'll end up having a function named the same as an std function.. You might discover that as soon as you write it, or you might write your function and then later include the header file with the std version of the function, at which point your application will mysteriously break.
A namespace name is the name of a namespace.
namespace A {
}
namespace B = A;
The statement says that using a namespace name without a using declaration is an error. But that's not true: The above code is fine, still using the namespace-name A as an unqualified name.
Probably it should say the following, to convey its meaning
"Using an unqualified version of a namespace member name without a using declaration
outside the scope of the namespace is an error, although some compilers may fail to
detect this error"
Mentioning the scope is important. The following, for example, is fine too, even though it uses the unqualified version of a namespace member name
namespace A {
int x;
int &y = x; // x is an unqualified name
}
Books should be careful to try and not use slippery language. And even outside the scope of the namespace, the above sentence is not entirely correct because you can also extend the scope of x by a using directive. Using declarations aren't the only way to name a namespace member outside the namespace using an unqualified name.

What is the need to specify "std" prefix?

I'm a beginner in programming and I often see many programs using the prefix std if they are using any std functions like std::cout, std::cin, etc. I was wondering what is it's purpose ? Is it just a way of good programming or is there more to it ? Does it make any difference for the compiler or is it readability or what ? Thanks.
The STL types and functions are defined in the namespace named std. The std:: prefix is used to use the types without fully including the std namespace.
Option 1 (use the prefix)
#include <iostream>
void Example() {
std::cout << "Hello World" << std::endl;
}
Option #2 (use the namespace)
#include <iostream>
using namespace std;
void Example() {
cout << "Hello World" << endl;
}
Option #3 (use types individually)
#include <iostream>
using std::cout;
using std::endl;
void Example() {
cout << "Hello World" << endl;
}
Note: There are other implications to including an entire C++ namespace (option #2) other than not having to prefix every type / method with std:: (especially if done within a header) file. Many C++ programmers avoid this practice and prefer #1 or #3.
C++ has a concept of namespaces.
namespace foo {
int bar();
}
namespace baz {
int bar();
}
These two functions can coexist without conflict, since they're in different namespaces.
Most of the standard library functions and classes live in the "std" namespace. To access e.g. cout, you need to do one of the following, in order of preference:
std::cout << 1;
using std::cout; cout << 1;
using namespace std; cout << 1;
The reason you should avoid using is demonstrated with the above foo and baz namespaces. If you had using namespace foo; using namespace baz; any attempt to call bar() would be ambiguous. Using the namespace prefix is explicit and exact, and a good habit.
Nobody mentioned in their answer that a using namespace foo statement can be put inside a function body, thereby reducing namespace contamination in other scopes.
For example:
// This scope not affected by using namespace statement below.
void printRecord(...)
{
using namespace std;
// Frequent use of std::cout, io manipulators, etc...
// Constantly prefixing with std:: would be tedious here.
}
class Foo
{
// This scope not affected by using namespace statement above.
};
int main()
{
// This scope not affected either.
}
You can even put a using namespace foo statement inside a local scope (pair of curly braces).
It's a C++ feature called namespaces:
namespace foo {
void a();
}
// ...
foo::a();
// or:
using namespace foo;
a(); // only works if there is only one definition of `a` in both `foo` and global scope!
The advantage is, that there may be multiple functions named a - as long as they are within different namespaces, they can be used unambiguously (i.e. foo::a(), another_namespace::a()). The whole C++ standard library resides in std for this purpose.
Use using namespace std; to avoid the prefix if you can stand the disadvantages (name clashes, less clear where a function belongs to, ...).
It's short for the standard namespace.
You could use:
using namespace std
if you don't want to keep using std::cout and just use cout