explicit qualification in declaration of 'std::cout' - c++

The problem is really strange for me.
The code is as simple as possible:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
It is just helloworld as it is created from standart cpp project.
I am sure it was worked. But after some time (really don't remember what have chaged...) I got an error:
error: explicit qualification in declaration of 'std::cout'
extern ostream std::cout; /// Linked to standard output
funny thing that is not in the project but inside iostream
some help? ^_^
.new information.:
I was building boost library and for many of files I am getting the same error: explicit qualification in declaration of 'std::cout'
I use MinGW

The only explanations that come to mind are:
Someone modified the standard header (accidentally?), replacing the original
extern ostream cout;
with incorrect
extern ostream std::cout;
Someone defined a macro named cout as std::cout, most likely in the compiler's command line. E.g.
-Dcout=std::cout
See http://coliru.stacked-crooked.com/a/bc5be8c7d99fed53 for example.

Related

I am attempting to write my first "Hello World" code in Visual Studio 2013. Why am I received a "IntelliSense: no operator message" & "error C2563"?

#include <istream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main() {
count <<"Hello World! I am C++ Program." <<endl;
return 0;
}
IntelliSense: no operator message Line 7, Column 8
error C2563:mismatch in formal parameter list Line 7, Column 1
Replace #include <istream> with the correct header, #include <iostream>.
Helpful mnemonic:
io = input/output
stream = "stream of data"
Additionally, the name of the standard output stream is std::cout or cout with the std:: namespace scope removed.
Helpful mnemonic:
std:: = Standard library's
cout = console output
The problems you are having with your simple block of code is simply: spelling errors.
Firstly, you have misspelled the input/output stream file in your include statement, so you need to rename the header file to:
#include <iostream>
There is no heade file named istream.
Secondly, you also misspelled the cout function to count. Change that line to:
cout << "Hello World! I am C++ Program." << endl;
Those lines should work now.
Also a recommendation for your future programs; avoid using the line
using namespace std;
Why? Because as you move on to more complex programming, you will undoubtedly learn and begin to define a data type or variable, and sometimes, that name may also be used by the standard library. As a result, you will have a hard time trying to differentiate the variables or data types you defined and the ones defined in the std library.
Therefore, try and attach std:: before every function that is a part of the standard library.
EDIT:
The code you posted in the comments box is pretty unreadable, so I just fixed it and have posted it below:
#include <iostream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main()
{
cout <<"Hello World! I am C++ Program." <<endl;
return 0;
}
I've tried this in my IDE and fixed with the same and only recommendations from above. It works for me.

What is the relationship between iostream and namespace std?

I am currently using Teach Yourself C++ in 21 Days, Second Edition book to learn about C++ coding, along with Microsoft Visual C++ 2010 Express. At the end of Chapter 1, there is a small exercise about writing and compiling the following code:
#include <iostream>
int main ()
{
cout << "Hello World!\n";
return 0;
}
Quite simple, right? However to my surprise the code would not compile, due to this error:
error C2065: 'cout' : undeclared identifier
I began scouring the Web, and soon found some solutions here. Turns out I had to add
using namespace std; to my code!
However there was no mention of namespaces in the book, so I figured the book is outdated. (It uses #include <iostream.h> pre-processor directive!) After some more Web research I found a lot of information about namespaces, namespace std, along with some historical background on <iostream.h> and <iostream>, and all this flow of new information is quite confusing to me. (Not to mention all the unnecessary Google results about medical STDs...)
So here are some questions I've got so far:
If I am including the iostream library, why is a namespace needed to find cout? Is there another cout somewhere that could cause a name clash? If someone could provide a diagram for this, that'd be great.
And as a bonus, some historical background:
What exactly was iostream.h before it was changed to iostream?
Did namespace play a part in this change?
All of the standard library definitions are inside the namespace std. That is they are not defined at global scope, so in order to use them you need to qualify them in one of the following way:
std::cout
using namespace std
using std::cout
For instance lets take this:
// declarations
int global_variable;
namespace n {
int variable2;
}
global_variable can be access as it is:
int x;
x = global_variable;
But variable2 is not part of the global space, but part of the namespace n.
int x;
x = variable2; // error variable2 identifier not found.
So you have to use the fully qualified name:
int x;
x = n::variable2;
As a shortcut you can write:
using namespace n;
int x;
x = variable2; // variable2 is searched in current namespace
// and in all namespaces brought in with using namespace
// Found ONLY in namespace n -> OK
or
using n::variable2; // this makes any unqualified reference to `variable2`
// to be resolved to `n::variable2`
int x;
x = variable2;
As for the header files, iostream.h was used by many compilers before there was a standard. When the committee tried to standardize they decided to make the C++ headers extensionless in order not to break compatibility with existing code.
Because this line starts with #, it is called a "preprocessor directive". The preprocessor
reads your program before it is compiled and only executes those lines beginning with #. The preprocessor sets up your source code for the compiler.
The #include directive causes the preprocessor to include the contents of another file into the program. The iostream file contains code that allows a C++ program to display output to the screen and take input from the keyboard. The iostream files are included in the program at the point the #include directive appears. The iostream is called a header file and appears at the top or head of the program.
using namespace std;
C++ uses namespaces to organize names or program entities. It declares that the program will be assessing entities who are part of the namespace called "std." Every name created by the iostream file is part of that namespace.
1.If I am including the iostream library, why is a namespace needed to find cout? Is there another cout somewhere that could cause a name clash?
It is needed because the C++ standard requires that cout be inside the std namespace. There could be a clashing cout, but not in the standard library (e.g. your own code, or some third party library.)
1.What exactly was iostream.h before it was changed to iostream?
It could be anything, because it is not part of the standard, but it was the name of a pre-standardization header which formed the basis for iostream. Usually, it declared all names in the global namespace, so it is likely that the example you are looking at was written pre-standardization.
2.Did namespace play a part in this change?
This question is unclear. The keyword namespace may be used inside implementations, and it is used to declare and define data, functions, types etc. inside a namespace. So it did have some part in this change.
namespace foo
{
void bar(); // declares foo::bar
}
In C++, you can logically group identifiers into namespaces.
cout stream is inside namespace std. You can use it in 3 ways.
Write using namespace std at top and use cout as you did.
Write using std::cout at top and use cout as you did.
Use std::cout instead of cout
Thanks to #bolov ..to understand the point referring this standard, this is the declaration:
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
namespace std
{
extern istream cin;
extern ostream cout;
extern ostream cerr;
extern ostream clog;
extern wistream wcin;
extern wostream wcout;
extern wostream wcerr;
extern wostream wclog;
}
With new version of c++ namespace was included. iostream contains all the declarations for input and output. Namespace std is used to tell that we are using cout and cin which were part of std namespace. You can create your own variables named cout and cin in your own namespace.
I had the same question as you. I am just going to teach you in laymen terms.
Imagine that you need a pencil which is placed in a drawer which is present in your bedroom. So you need to enter in your room to access your pencil. Here room is iostream. After you entered your room, you need to open the drawer and access the pencil. Here drawer is namespace and pencil is cin/cout.
Reference:- https://en.wikiversity.org/wiki/C%2B%2B/Introduction

How to link C++ source files with Code::Blocks

I'm doing something wrong, I know. I can't quite figure out how to
link two .cpp files together through a header file. The calling
method can't see the other source.
I'm using Code::Blocks as an IDE with MinGW.
Any help would be greatly appreciated. It would be even more
appreciated if you could show the fixed source, link in the reply to a
pastebin page with it.
/***********************************main.cpp***********************************/
#include <iostream>
using namespace std;
#include "test.h"
int main()
{
printTest(); //can't see printTest, defined in test.cpp
return 0;
};
/***********************************test.h***********************************/
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
void printTest();
#endif // TEST_H_INCLUDED
/***********************************test.cpp***********************************/
#include "test.h"
void printTest()
{
cout << "Hello world!" << endl;
};
You might find this code blocks wiki helpful. It looks like Code blocks uses a managed build system so if you add the file to the project properly then it should know to compile it and link in the object file that results.
And just to be more explicit about some other comments, when you use "using namespace std;" the namespace is only brought into scope for the file where the using statement is located. That is why others are telling you to explicitly specify the std:: namespace. You could also bring all of the std namespace into scope in the test.cpp file. Many people consider this a bad habit to get into. It's generally better to bring into scope just what you need via
using std::cout;
using std::endl;
Finally, remember that std::endl adds a new line AND flushes the buffer, it's not a good replacement for a new line character in all cases.
In test.cpp replace cout << "Hello world!" << endl;
by std::cout << "Hello world!" << std::endl;
sanket answer’s seems incomplete to me.
You need to add #include <iostream> in your test.cpp so that the compiler knows what "cout" is.
As sanket stated, you should use std::cout and std::endl in test.cpp.

Dev-C++ compile errors

I want to use "Dev-C++" for compile c++ codes.
So I download and install it, and write this code:
#include <iostream.h>
main () {
cout << "124";
}
but when I compiled it, it said:
In file included from
E:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from [myfile path]\Untitled1.cpp:1:
E:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2:
warning: #warning This file includes
at least one deprecated or antiquated
header. Please consider using one of
the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the header
for the header for C++ includes,
or instead of the
deprecated header . To
disable this warning use
-Wno-deprecated.
After I saw errors, I change my code to this code:
#include <iostream>
main () {
cout << "124";
}
but it said again that errors.
I compile first code easily in Turbo C++, BUT in Dev-C++ ...
What can I do?
First, make sure you write out the full definition of main, including the int return type. Leaving out the return type is an old, antiquated practice which doesn't fly these days.
Second, in the new-style headers—the ones missing the .h extension—the standard library is under the std namespace. There are two ways to make your program work:
1. Add an std:: qualifier to cout.
#include <iostream>
int main () {
std::cout << "124";
}
2. Add a using declaration to allow unqualified references to the std namespace.
#include <iostream>
using namespace std;
int main () {
cout << "124";
}
Make sure you put int in front of main () {
I believe any C/C++ program's main() function is required by POSIX and the appropriate language standards to return an int (someone correct me if I'm wrong).
EDIT: Also, be sure to include using namespace std; above int main ().

"nice" keyword in c++?

So I was doing some simple C++ exercises and I noticed an interesting feat. Boiled down to bare metal one could try out compiling the following code:
class nice
{
public:
nice() {}
};
int main()
{
nice n;
return 0;
};
The result is a compilation error that goes something like this:
<file>.cpp: In function ‘int main()’:
<file>.cpp:11: error: expected `;' before ‘n’
<file>.cpp:11: warning: statement is a reference, not call, to function ‘nice’
<file>.cpp:11: warning: statement has no effect
And this was using regular g++ on Max OS X, some of my friends have tried in on Ubuntu as well, yielding the same result.
The feat seems to lie in the word "nice", because refactoring it allows us to compile. Now, I can't find the "nice" in the keyword listings for C++ or C, so I was wondering if anyone here had an idea?
Also, putting
class nice n;
instead of
nice n;
fixes the problem.
P.S. I'm a relative C++ newbie, and come from the ActionScript/.NET/Java/Python world.
Update:
Right, my bad, I also had an
#include <iostream>
at the top, which seems to be the root of the problem, because without it everything works just fine.
Maybe the problem is somehow caused by function nice in libc. It is similar to trying to name your class printf.
using namespace std, by any chance?
Edit:
The standard says that standard headers define all their symbols in namespace std (see 17.4.1.2.4).
A footnote, however, also says that the <.h> variants dump their names into the global namespace - but of course no one should be using these ;)
It is a namespace problem but not with namespace std. The header <iostream> is pulling in <unistd.h>
If you try
class nice
{
public:
nice() {};
};
int main(int argc, char *argv[])
{
nice n;
return 0;
}
there is no problem.
Simply add
#include <unistd.h>
and you will get the "expected ‘;’ before ‘n’" error. Namespace std does not enter the picture.
So the solution is the same as before - put class nice in its own namespace and it will not clash with the global ::nice().
Try this version:
#include <iostream>
namespace test
{
class nice
{
public:
nice() {}
};
};
using namespace std;
int main()
{
test::nice n;
cout << "well I think this works." << endl;
return 0;
}
In this case I've defined my own namespace test. Doing so, I can use whatever class names I like, including functions already defined like printf. The only things I can't re-use are reserved words like int or namespace.
Note: if you say:
using namespace test;
As well and refer to nice alone, you'll get this error:
nice.cpp: In function ‘int main()’:
nice.cpp:18: error: reference to ‘nice’ is ambiguous
/usr/include/unistd.h:593: error: candidates are: int nice(int)
nice.cpp:7: error: class test::nice
Which I think explains nicely what's going on - nice now exists in two namespaces and the compiler can't work out which one you mean.
It works fine for me. Did you try the exact code you posted?
extern "C"
{
#include <unistd.h>
}