I am working on the \'driver\' part of my programing assignment and i keep getting this absurd error:
error C2065: \'cout\' : undeclared identifier
write this code, it works perfectly..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
In Visual studio use all your header filer below "stdafx.h".
The include "stdafx.h"
is ok
But you can't use cout
unless you have included using namespace std
If you have not included namespace std you have to write std::cout
instead of simple cout
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
#include <iostream>
using namespace std;
int bob (int a) { cout << "hey" << endl; return 0; };
int main () {
int a = 1;
bob(a);
return 0;
}
In VS2017, stdafx.h
seems to be replaced by pch.h
see this article,
so use:
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "Enter 2 numbers:" << endl;
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.