新项目,C ++控制台应用程序无法编译 [英] New Project, C++ console application cannot compile

查看:91
本文介绍了新项目,C ++控制台应用程序无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Vstudio 2003 .NET在cli中学习c ++. 我知道每当我开始一个新项目时,我的代码就会显示为

I am using Vstudio 2003 .NET in learning c++ in cli
I know when ever I start a new project my code comes up as

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}



但是当我启动它时,我希望它像
那样准备好



But when I start it I want it to come up prepared like

#include "stdafx.h"
#include <conio.h>
using namespace std

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}




原因是当我仅使用stdafx进行编译而没有std nsamespace时,我得到

崩溃




cause when I compile with just stdafx, and without std nsamespace, i get

Collapse

// cout.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Hello" << endl;
    _getch();
    return 0;
}



崩溃

错误C2065:"endl":未声明的标识符
错误C2065:"cout":未声明的标识符



Collapse

error C2065: ''endl'' : undeclared identifier
error C2065: ''cout'' : undeclared identifier

推荐答案

Herboren写道:
Herboren wrote:

#include"stdafx.h" #include< conio.h>
使用命名空间std

int _tmain(int argc,_TCHAR * argv [])
{
返回0;
}

#include "stdafx.h" #include <conio.h>
using namespace std

int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}



您忘记了std后的分号.
顺便说一句,如果您没有包含任何std标头,那么using指令的用途是什么?
:)



You forgot a semicolon after std.
BTW What is the purpose of the using directive if you haven''t any std header included?
:)


虽然这不能回答您的问题,但是您无需为调用的每个名称空间添加using namespace.实际上,这样做是在破坏为什么首先引入这些名称空间的原因:避免全局名称空间的混乱.

如果没有using语句,您仍然可以通过添加名称空间的名称作为前缀来访问单个符号,如下所示:
While this doesn''t answer your question, you do not need to add using namespace for every namespace you invoke. In fact, by doing so you''re undermining the very reason why these namespaces have been introduced in the first place: avoiding the cluttering of the global namespace.

Without the using statement you can still access individual symbols just by adding the name of the namespace as a prefix like this:
std::cout << "hello" << std::endl;



如果经常使用它们,甚至可以将其他名称空间中的单个对象或类型注入到自己的对象中:



You can even inject singular objects or types from other namespaces into your own if you use them often:

#include <io>
typedef std::ostream ostream; // creates ''ostream'' as a symbol in global namespace
ostream& cout(std::cout); // creates cout as a reference to std::cout

void hello() {
    cout << "hello" << std::endl; // uses ::cout defined above
}



我并不是说您永远都不应该利用using语句,我只是认为将您的IDE添加到每个项目中不是一个好主意!

此外,将其添加到您的main.cpp文件中对其他cpp文件无济于事.如果您确实希望这样做影响项目中的所有文件,请改为将其添加到stdafx.h.



I''m not saying you shouldn''t take advantage of the using statement ever, I just think it''s a bad idea to manipulate your IDE into adding it into every project!

Besides, adding this into your main.cpp file doesn''t help for other cpp files. If you really want this to affect all files in your project, add it to stdafx.h instead.


回答后续问题(在之后):

使用cout必需的stdiostream标头:

Answering follow-up Question (after ):

Using cout required std and iostream header:

#include <iostream>
using namespace std;

//...</iostream>



—SA



—SA


这篇关于新项目,C ++控制台应用程序无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆