error C2065: ‘cout’ : undeclared identifier

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.

Leave a Comment