c 语言未声明的标识符cout,错误C2065:“ cout”:未声明的标识符

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;

}

write this code, it works perfectly..

#include "stdafx.h"

#include

using namespace std;

int main()

{

cout<

return 0;

}

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;

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

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.

If you started a project requiring the #include "stdafx.h" line, put it first.

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;

}

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?

I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.

Such a silly solution in my case:

// Example a

#include

#include "stdafx.h"

The above was odered as per example a, when I changed it to resemble example b below...

// Example b

#include "stdafx.h"

#include

My code compiled like a charm. Try it, guaranteed to work.

before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;.

Little by little add to it to find your issue.

cout << "hi" << endl;

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?)

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;

}

I had to include stdafx.h as it caused an error not having it in.

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.

Are you sure it's compiling as C++? Check your file name (it should end in .cpp). Check your project settings.

There's simply nothing wrong with your program, and cout is in namespace std. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.

I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated test.cpp file didn't have a main() function.

I've got a really, really bad feeling about VS 2010.

Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.

#include "stdafx.h"

#include

using namespace std;

void main( )

{

int i = 0;

cout << "Enter a number: ";

cin >> i;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值