C++ – error C2065: ‘cout’ : undeclared identifier

cnamespacesstdvisual studio 2010

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:

error C2065: 'cout' : undeclared identifier

I have even tried using the std::cout but i get another error that says: IntelliSense: namespace "std" has no member "cout" when i have declared using namespace std, included iostream + i even tried to use ostream

I know it's a standard noob question but this has stumped me and I'm a novice (meaning: I've programed before…)

#include <iostream>
using namespace std;

int main () {
    cout << "hey" << endl;
 return 0;
}

I'm using Visual Studio 2010 and running Windows 7. All of the .h files have "using namespace std" and include iostream and ostream.

Best Answer

In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:

These will not work.

#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}




#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}

This will do.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
    cout << "hey" << endl;
    return 0;
}

Here is a great answer on what the stdafx.h header does.

Related Topic