I wrote the following code after using "gedit take_input.cpp":
#include <iostream>
int main()
{
cout<<"please enter your name (followed by 'enter')\n";
string file;
cin >> file;
cout<<"hello" << file << " ! welcome to ilinux, where innovation is a promise\n";
}
However, when I used "g++" to convert my human-readable code into object code (writing g++ take_input.cpp -o take_input), the terminal returns with a result similar to this:
take_input.cpp: In function ‘int main()’:
take_input.cpp:5:1: error: ‘cout’ was not declared in this scope
cout<<"please enter your name (followed by 'enter')\n";
^
take_input.cpp:5:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:61:18: note: ‘std::cout’
extern ostream cout; /// Linked to standard output
^
take_input.cpp:7:1: error: ‘string’ was not declared in this scope
string file;
^
take_input.cpp:7:1: note: suggested alternative:
In file included from /usr/include/c++/4.9/iosfwd:39:0,
from /usr/include/c++/4.9/ios:38,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from take_input.cpp:1:
/usr/include/c++/4.9/bits/stringfwd.h:62:33: note: ‘std::string’
typedef basic_string<char> string;
^
take_input.cpp:9:1: error: ‘cin’ was not declared in this scope
cin >> file;
^ ^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
take_input.cpp:9:1: note: suggested alternative:
In file included from take_input.cpp:1:0:
/usr/include/c++/4.9/iostream:60:18: note: ‘std::cin’
extern istream cin; /// Linked to standard input
^
take_input.cpp:9:8: error: ‘file’ was not declared in this scope
cin >> file;
^
Could you tell me what the reason is?
The errors that you are getting are because the cout is not in the global namespace rather it is in std namespace.
Well instead of writing
using namespace std;
after #include <iostream> try using:
using std::cout;
since using first option is a bad practice. You can refer to Why is using namespace std is a bad practice.
For benefits of using using std::cout refer Using std namespace
Also you can use std::cout everywhere if you don't want to use using std::cout.
Just read the error messages that you compiler gave you. The problem is that
‘cout’ was not declared in this scope
And the "suggested alternative" is std::cout. Same goes for string vs. std::string.
Note that generally, things belonging to the standard library need to be qualified with std:: to be found.
You also need to #include <string> to use std::string btw.
The compiler gives you the answer on line 7: Since you're not using the std namespace, you have to prepend std:: to your cout and cin calls.
Just add
using namespace std;
after #include <iostream>
Try this out.
Related
I wrote a simple hello world program:
#include<bits/stdc++.h>
using namespace std;
int main(){
cout<<"Hello";
}
But it is reporting an error:
In file included from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_algobase.h:64,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\char_traits.h:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ios:40,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\istream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\sstream:38,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\complex:45,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\ccomplex:39,
from c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\mingw32\bits\stdc++.h:54,
from AAY.cpp:1:
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:214:11: error: expected unqualified-id before numeric constant
214 | _T1 7
| ^
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h: In constructor 'constexpr std::pair<_T1, _T2>::pair()':
c:\mingw\lib\gcc\mingw32\9.2.0\include\c++\bits\stl_pair.h:245:9: error: class 'std::pair<_T1, _T2>' does not have any field named 'first'
245 | : first(), second() { }
and so on.
Basically, it says that std::pair does not have a member named first although I haven't used any. Does anyone have a solution?
You should not use
#include<bits/stdc++.h>
This is not a C++ compliant header file.
Please use #include <iostream> instead and it should be sufficient.
Of course you may have also a problem with your compiler installation.
In an old project code was a statement like:
cout.operator<<("Hi...");
ostream.operator<<("Hi....");
It argued that it can be written as followa, resulting in same output:
cout << "Hi..";
I have tried to put these statement in main(). But compiler gives error:
In function 'int main()':
11:3: error: 'ostream' was not declared in this scope
11:3: note: suggested alternative:
In file included from /usr/include/c++/4.9/ios:38:0,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from 2:
/usr/include/c++/4.9/iosfwd:136:33: note: 'std::ostream'
typedef basic_ostream<char> ostream;
The other thing that I am not convinced of is this statement that ostream can directly call operator<<. Same about cout that can it call operator<< directly.
If some body could elaborate or explain how, then I will able to debug the code.
Thanks.
The code line in question can actually ber compiled and does work, e.g:
#include <iostream>
using namespace std;
int main (void)
{
cout << "Hi"; // code line in question
cout << endl;
return 0;
}
The quoted error is caused by using ostream, which is the type of cout, in a place where an object (not a type) is required.
As also mentioned in a comment, the fix of that error is using an object of that type instead. Which basically means the first line of quoted code, i.e. the line in the solution code above.
When i compile my c++ code using g++ 5.1.1 it says
"narc05b.cpp: In function ‘int main()’:
anarc05b.cpp:5:3: error: ‘cout’ was not declared in this scope
cout<<"hello\n"; ^ anarc05b.cpp:5:3: note: suggested alternative:
In file included from anarc05b.cpp:1:0:
/usr/include/c++/5.1.1/iostream:61:18: note: ‘std::cout’ extern
ostream cout; /// Linked to standard output "
...what does it mean ?
Use should use the namespace directive in the code
using namespace std;
or alternatively prefix cout with std::cout
I didn't understand why i take this "strange" error. I read similar questions but it didn't answer my questions. If i define the array inside main function rather than global scope, there is no error. But assume that i have to define this array in global scope. Why do i take this error?
Here is the code :
#include <iostream>
#include <cstring>
using namespace std;
int right[1005];
int main()
{
memset(right,0,sizeof(right));
return 0;
}
Here is the error :
memset2.cpp: In function ‘int main()’:
memset2.cpp:9:9: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
memset2.cpp:9:24: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
Namespace std has already name right and you included names form std in the global namespace by means of directive
using namespace std;
So to avoid the ambiguity use a qualified name
memset( ::right, 0, sizeof( ::right ) );
Or remove the directive and in this case you may use unqualified name right because the compiler will seek the name only in the global namespace.
remove using namespace std ; from your code and precede any standard function or object with std::
Today, after Slackware 13.37 installation, i've got the problem: default GCC 4.5.2 cannot compile my code. Now I study C++ by the Stephen Davis's book "C++ for dummies" and want to compile this:
#include <stdio.h>
#include <iostream.h>
int main(int nNumberofArgs, char* pszArgs[])
{
int nNCelsius;
cout << "Celsisus: ";
cin >> nNCelsius;
int nNFactor;
nNFactor = 212 - 32;
int nFahrenheit;
nFahrenheit = nNFactor * nNCelsius / 100 + 32;
cout << "Fahrenheit: ";
cout << nFahrenheit;
return 0;
}
But my GCC 4.5.2 gives these errors:
FahTCel.cpp:7:14: error: expected ')' before ';' token
FahTCel.cpp:7:14: error: 'main' declared as function returning a function
FahTCel.cpp:8:1: error: 'cout' does not name a type
FahTCel.cpp:9:1: error: 'cin' does not name a type
FahTCel.cpp:12:1: error: 'nNFactor' does not name a type
FahTCel.cpp:15:1: error: 'nFahrenheit' does not name a type
FahTCel.cpp:17:1: error: 'cout' does not name a type
FahTCel.cpp:18:1: error: 'cout' does not name a type
FahTCel.cpp:20:1: error: expected unqualified-id before 'return'
FahTCel.cpp:21:1: error: expected declaration before '}' token
Three errors:
The correct header is <iostream>. This program requires no other headers.
You must either put using namespace std; in the file, or refer to std::cout and std::cin explicitly. Take your pick, plenty of C++ programmers disagree about which of the two options is better. (You could also bring just cin and cout into your namespace, if you wanted.)
The program does not write a line terminator at the end. This will cause the output to "look bad" on most terminals, with the command prompt appearing on the same line as the output. For example:
Here are the corrections:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
...
cout << nFahrenheit << '\n';
...
}
Note: It is extremely unusual to see main take parameters with names other than argc and argv. Changing the names just makes it harder for other people to read your code.
its std::cout or you should add using namespace std;
and the include should be < iostream> not < ionstream.h>.