I have read that cout is an object of ostream...
But why does this code
#include<ostream>
using namespace std;
int main()
{
cout << "ostream included!" << endl;
return 0;
}
Throwing an error :-
practice1.cpp: In function 'int main()':
practice1.cpp:6:1: error: 'cout' was not declared in this scope
cout << "ostream included!" << endl;
^~~~
Am I going wrong in my understanding or is there some other fault?
(MinGW windows 10)
Thanks in advance!
Description
The reason of why this doesn't work is because cout is of type OStream but is inside the IOStream header. Hence, to get the definition of cout you need to include iostream library but not the ostream class.
Solution
Include iostream instead of ostream, as OriBS mentioned.
References
Object cout found in IOStream objects list
"Including iostream automatically includes also ostream..." see http://www.cplusplus.com/reference/iostream/
"The standard objects cout, cerr and clog are objects of this type." see http://www.cplusplus.com/reference/ostream/ostream/
You should include iostream
#include <iostream>
Related
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.
Here's the code....
#include <iostream>
int main()
{
cout << "WELCOME TO C++ PROGRAMMING";
return 0;
}
And when I go the terminal and pass the command..
g++ hello.cpp
It shows...
hello.cpp: In function ‘int main()’:
hello.cpp:4:2: error: ‘cout’ was not declared in this scope
cout << "WELCOME TO C++ PROGRAMMING";
^
hello.cpp:4:2: note: suggested alternative:
In file included from hello.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
So what's the reason? And what should I do?
cout is found in the std namespace.
#include <iostream>
int main()
{
std::cout << "Welcome";
return 0;
}
To avoid name collisions, the C++ library is included in a namespace, named std. So to get your program to compile, either you add:
using namespace std;
at the top of your program, either you prefix each "object" of the standard library with std:::
std::cout << "Welcome";
You should use std before cout
You should have:
std::cout << "Welcome";
So, I have the following code, and it builds and runs perfectly, tried various values and all is well. You'll notice I use log10 function and I do not include cmath or math.h. Why does it still build and run fine? Are those libraries really needed? Why/Why not? Does it have anything to do with me using visual studio? Like, would it not compile if say I was using a different IDE or command prompt to compile it?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Classify solutions as acidic or nonacidic" << endl<< endl;
//declaring double molar concentration
double mc = 1;
//using while and if statements to calculate pH in fixed notaion and acidic or nonacidic
while (mc != 0)
{
cout << "Please enter a molar concentration (or enter 0 to exit): ";
cin >> mc;
if (mc != 0)
{
cout << "Molar Conentration = " << scientific << mc << endl; //scientific notation
double pH = -log10(mc);
cout << "pH = " << fixed << setprecision(6) << pH << endl; //6 deciumals
if (pH > 7)
{
cout << "Nonacidic" << endl << endl;
}
else if (pH < 7)
{
cout << "Acidic" << endl << endl;
}
else
cout << "Neutral" << endl << endl;
}
}
//end program when inputing 0
cout << "End of Program" << endl;
return 0;
}
The code:
i = i++ + ++i;
may also compile okay, but that doesn't make it a good idea :-)
It would be wise to include headers for library functions that you use. You don't lose any functionality by doing so but you do guarantee the functionality will work (misuse notwithstanding).
Detailed analysis follows.
Even if an implementation is relaxed about this, the standard mandates it. C++11 17.6.2.2 Headers /3 states:
A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header.
The gcc compiler, for example, will grumble bitterly about your code:
xyzzy.cpp: In function 'int main()':
xyzzy.cpp:22:34: error: 'log10' was not declared in this scope
double pH = -log10(mc);
^
As to why VC++ seemingly violates this rule, it has to do with the fact that header files are allowed to include other header files.
If you compile your code to produce pre-processor output (with /P), you'll find a line buried deep within it thus (at least in VS2013):
#line 1 "c:\\blah\\blah\\vc\\include\\cmath"
And a bit of analysis turns up the following hierarchy of includes:
iostream
istream
ostream
ios
xlocnum
cmath
(<xlocnum>, one of the internal headers used by <locale>, appears to need ldexp() from the <cmath> library, though there may be others as well).
That's further evidenced by the fact that VC++ does complain about the following code:
//#include <iostream>
using namespace std;
int main() {
double oneHundred = 100;
int two = log10 (oneHundred);
return two;
}
with:
error C3861: 'log10': identifier not found
but that error disappears the instant you uncomment the iostream inclusion line.
However, as previously stated, that is not behaviour you should rely on. If you are going to use a library function (or macro/template/whatever), it's up to you to include the correct header.
Otherwise your program compiling correctly is simply an accident.
As you've noticed, the code snippet you provided works in Visual Studio but not with other compilers. This is because of how the standard library is implemented for each compiler.
It turns out that when you include Visual Studio's implementation of <iostream>, you end up including a bunch of other headers indirectly, and one of these headers is <cmath>.
To see the exact chain, navigate to the standard library include directory. For me (I use Visual Studio 2013 Community Edition) this is located at
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
Open iostream. Note the line #include <istream>
Open istream. Note the line #include <ostream>
Open ostream. Note the line #include <ios>
Open ios. Note the line #include <xlocnum>
Open xlocnum. Note the line #include <cmath>
Guess what? You included cmath when you included iostream... so your code is good to go, on Visual Studio at least. But, don't rely on implementation details or your code will break if you try to migrate it to another platform/toolchain.
For example, trying to compile the provided snippet using g++ on Cygwin results in the following error:
temp.cpp: In function ‘int main()’:
temp.cpp:22:34: error: ‘log10’ was not declared in this scope
double pH = -log10(mc);
This must mean g++'s implementation of <iostream> doesn't depend on <cmath>
#include <iostream>
#include <iomanip>
int main ()
{
long double price;
std::cout << "Please, enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) std::cout << "Error reading price\n";
else std::cout << "The price entered is: " << price << '\n';
return 0;
}
I am getting error forlling error while compiling the above program.
getmoney.cpp: In function int main()':
getmoney.cpp:10: error:get_money' is not a member of `std'.
get_money is the predefined function. I don't know why i am receiving this error.
If you included iomanipbut nevertheless the compiler issues an error it means that your compiler does not support C++ 2011 standard or you did not select such an option of the compiler.
Did you include the header "iomanip" in your code?
Wen you have such problem go to check on a reference website such as http://www.cplusplus.com/reference/iomanip/get_money/
and you will see that
This manipulator is declared in header <iomanip>.
and you even often have an example code which show you how to use it.
So including iomanip may solve your problem
If that is not working, you are probably using an older compiler which does not support C++ 11. And therefore you could simply not use this function without changing your compiler.
I am trying to compile the simple program below. But, it's not compiling & gives error:
error C2065: 'cout' : undeclared identifier
I want to ask you that why this program doesn't work though I've included iostream header file in it?
#include <iostream>
void function(int) { cout << “function(int) called” << endl; }
void function(unsigned int) { cout << “function(unsigned int) called” << endl; }
int main()
{
function(-2);
function(4);
return 0;
}
Thanks in advance.
The cout stream is defined in the std namespace. So to name it you write:
std::cout
If you want to shorten this to cout then you can write
using namespace std;
or
using std::cout;
before writing cout.
Any good documentation source will tell you which namespace contains an object. For instance: http://en.cppreference.com/w/cpp/io/cout
You have to write std::cout or add using std;