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
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.
I'm looking up c++ library, and see the istream class, I am confused with a contractor with an address symbol. what is the meaning of a constructor with an address symbol?
one of the istream constructors is.
protected: iostream& (iostream&& x);
I found it in website cplusplus.com,
link: iostream
I defined a customer class with a similar constructor that has a & symbol:
//Test.cpp
#include <iostream>/*cout,cin*/
#include <typeinfo>/*typeid(),name()*/
using namespace std;
struct MyTest{
MyTest&(double b){}
};
int main(int argc,char* argv[]){
MyTest mt2(2.1);
cout << typeid(mt2).name() << endl;
return 0;
}
I use the below command to compile it:
g++ Test.cpp -o Test -std=c++11
however, I get some compile error messages:
Test.cpp:7:11: error: expected unqualified-id before ‘float’
MyTest&(float b){}
^
Test.cpp:7:11: error: expected ‘)’ before ‘float’
Test.cpp:7:10: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:17: error: expected ‘;’ at end of member declaration
MyTest&(float b){}
^
Test.cpp:7:18: error: expected unqualified-id before ‘)’ token
MyTest&(float b){}
^
Test.cpp: In function ‘int main(int, char**)’:
Test.cpp:12:16: error: no matching function for call to ‘MyTest::MyTest(double)’
MyTest mt2(2.1);
I got confused, c++ library istream class is fine. why did my custom class constructor fail? what am I missing?
The information on cplusplus.com is... sometimes not dependable. (See What's wrong with cplusplus.com? for a discussion of this.) On CPPReference, you can see that the move constructor is, you know, just a regular move constructor.
This is a bug in http://www.cplusplus.com/reference/istream/iostream/iostream/.
If you look at https://en.cppreference.com/w/cpp/io/basic_iostream/basic_iostream, you will find
protected: basic_iostream( basic_iostream&& other );
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.
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::
The File is at the location /home/shivang/Desktop and the filename is sh1.cpp
Source code for the file is given below
#include iostream
#include json/json.h
#include json/reader.h
using namespace std;
using namespace Json;
int main() {
std::string example = "{\"array\":[\"item1\", \"item2\"], \"not an array\":\"asdf\"}";
Value value;
Reader reader;
bool parsed = reader.parse(example, value, false);
std::cout << parsed;
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
The following error messages are displayed.
/home/shivang/Desktop/sh1.cpp: In function ‘int main()’:
/home/shivang/Desktop/sh1.cpp:10:2: error: ‘Value’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:10:8: error: expected ‘;’ before ‘value’
/home/shivang/Desktop/sh1.cpp:11:2: error: ‘Reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:11:9: error: expected ‘;’ before ‘reader’
/home/shivang/Desktop/sh1.cpp:13:16: error: ‘reader’ was not declared in this scope
/home/shivang/Desktop/sh1.cpp:13:38: error: ‘value’ was not declared in this scope
Configuration gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)
jsoncpp-src-0.5.0
eclipse-cpp-helios-SR2-linux-gtk
I have never used Json or C++ before. But a little googling around led me to this page. I think adding the following line to your list of includes should help:
#include <json/value.h>