可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 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.
回答1:
In Visual Studio you must #include "stdafx.h"
and be the first include of the cpp file. For instance:
These will not work.
#include using namespace std; int main () { cout << "hey" << endl; return 0; } #include #include "stdafx.h" using namespace std; int main () { cout << "hey" << endl; return 0; }
This will do.
#include "stdafx.h" #include using namespace std; int main () { cout << "hey" << endl; return 0; }
回答2:
write this code, it works perfectly..
#include "stdafx.h" #include using namespace std; int main() { cout<<"Hello World!"; return 0; }
回答3:
I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.
# include "stdafx.h" # include using namespace std;
回答4:
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
回答5:
I have seen that if you use
#include
then you will get the problem.
If you use
#include
(notice - without the .h)
then you will not get the problem you mentioned.
回答6:
If you started a project requiring the #include "stdafx.h"
line, put it first.
回答7:
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
#include using namespace std; int bob (int a) { cout << "hey" << endl; return 0; }; int main () { int a = 1; bob(a); return 0; }
回答8:
If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?
回答9:
I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used std
plenty without issues. Try typing:
std::
And see if Intellisense gives you anything. If it gives you the usual stuff (abort
, abs
, acos
, etc.), except for cout
, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.
Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an include
; it could be that something stuck the parser and it didn't "find" cout
right away. (In which case, try restarting VS maybe?)
回答10:
Take the code
#include using namespace std;
out of your .cpp file, create a header file and put this in the .h file. Then add
#include "whatever your header file is named.h"
at the top of your .cpp code. Then run it again.
回答11:
I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:
#include "stdafx.h" #include using namespace std; int main() { cout << "hey" << endl; return 0;