0

Why this program shows error?

#include <iostream> int counter=50; int main() { register int counter; for(counter=1;counter<10;counter++){ cout<<endl<<::counter/counter; } return 0; } main.cpp:15:5: error: ‘cout’ was not declared in this scope cout<<endl<<::counter/counter; ^~~~ main.cpp:15:5: note: suggested alternative: In file included from main.cpp:9:0: /usr/include/c++/6/iostream:61:18: note: ‘std::cout’ extern ostream cout; /// Linked to standard output ^~~~ main.cpp:15:11: error: ‘endl’ was not declared in this scope cout<<endl<<::counter/counter; ^~~~ main.cpp:15:11: note: suggested alternative: In file included from /usr/include/c++/6/iostream:39:0, from main.cpp:9: /usr/include/c++/6/ostream:590:5: note: ‘std::endl’ endl(basic_ostream<_CharT, _Traits>& __os) ^~~~

13th Jul 2021, 1:20 AM
Durga M
Durga M - avatar
2 Réponses
+ 4
The error message says it " 'cout was not declrared....'" "suggested alternative: ..... 'std::cout' " 'cout' and 'endl' are defined in the std namespace and you need to access them from there, like 'std::cout' and 'std::endl'. Alternatively, just bring the members of the std namespace into scope by putting this line before main() or as the first line of main() `using mamespace std;`
13th Jul 2021, 1:46 AM
XXX
XXX - avatar
+ 1
You can also define reference of `std` namespace for any of its specific member using std::cout, std::endl; There's another warning there after the above steps, stating C++17 support for `register` storage specifier, so you might wanna remove that `register` and just define <counter> as follows int counter {}; Or let <counter> be only inside the for..loop scope, since local <counter> is only used within loop body. for( int counter {1}; counter < 10; counter++ ) { // loop body }
13th Jul 2021, 3:52 AM
Ipang