namespace – error: ‘vector’ has not been declared, ‘cout’ was not declared in this scope

If you are newbie in programming cpp programs and trying out simple ‘cout’, ‘endl’ operations or trying out your hands on vectors etc. And you found some same error patterns given below.

isoStandardC.cpp:73: error: ‘cout’ was not declared in this scope
isoStandardC.cpp:73: error: ‘endl’ was not declared in this scope
isoStandardC.cpp:76: error: ‘cout’ was not declared in this scope
isoStandardC.cpp:76: error: ‘endl’ was not declared in this scope
isoStandardC.cpp:77: error: ‘ifstream’ was not declared in this scope
isoStandardC.cpp:25: error: ‘vector’ has not been declared

isoStandardC.cpp:25: error: expected ‘,’ or ‘…’ before ‘<’ token isoStandardC.cpp: In function ‘int main(int, char**)’: isoStandardC.cpp:118: error: cannot convert ‘std::vector, std::allocator > > >’ to ‘int’ for argument ‘2’ to ‘void binString(int, int)’

Then you missed putting ‘namespace‘ in the starting of the program. Confused???

Why we need to use namespace std in cpp??

C++ provides one global namespace. Namespaces allow to group entities like classes, objects and functions under a name. The basic structure for defining a custom ‘namespace’ is given below:

namespace myNameSpace{
int x, y;
}

you can access the integer variable using scope operator ::

myNameSpace::x
myNameSpace::y

So, now the solution for the above error message, you need to use this at the top of the line program or header file

using namespace std;

It tells the compiler to treat the names in the standard library, a very important part of the C++ language definition.

You can also be specific to vectors: using namespace std::vector
Even you DON’T have to use “using namespace std;” in C++. It just makes typing easier. Here are two identical programs, one with, and another one without.

/*With using namespace declaration*/
#include <stdlib.h>
using namespace std;
int main () {
cout << "Hello World of C++ programming!" << endl;
return 0;
}

/*Without explicitly declaring namepace */
#include <stdlib.h>
int main () {
std::cout << “Hello World of C++ programming!” << std::endl;
return 0;
}

For more  detail you can refer: LINK1, LINK2, LINK3

  1 comment for “namespace – error: ‘vector’ has not been declared, ‘cout’ was not declared in this scope

  1. Anonymous
    May 10, 2012 at 11:53 pm

    good article

Comments are closed.

Animated Social Media Icons Powered by Acurax Wordpress Development Company