简体   繁体   English

来自简单Visual C ++项目的编译器错误

[英]Compiler errors from simple Visual C++ project

I'm trying to make program run. 我正在尝试使程序运行。 When I try Hello World in win32 project/.cpp file I get bunch of errors 当我在win32 project / .cpp文件中尝试Hello World时,出现一堆错误

1>------ Build started: Project: HelloWorld3, Configuration: Debug Win32 ------ 1>Compiling... 1>hello.cpp 1>...\\projects\\helloworld3\\helloworld3\\hello.cpp(7) : error C2065: 'cout' : undeclared identifier 1>...\\projects\\helloworld3\\helloworld3\\hello.cpp(7) : error C2001: newline in constant 1>...\\projects\\helloworld3\\helloworld3\\hello.cpp(8) : error C2143: syntax error : missing ';' 1> ------构建开始:项目:HelloWorld3,配置:调试Win32 ------ 1>正在编译... 1> hello.cpp 1> ... \\ projects \\ helloworld3 \\ helloworld3 \\ hello。 cpp(7):错误C2065:'cout':未声明的标识符1> ... \\ projects \\ helloworld3 \\ helloworld3 \\ hello.cpp(7):错误C2001:换行符为常量1> ... \\ projects \\ helloworld3 \\ helloworld3 \\ hello.cpp(8):错误C2143:语法错误:缺少';' before 'return' 1>Build log was saved at "file:/...\\Projects\\HelloWorld3\\HelloWorld3\\Debug\\BuildLog.htm" 1>HelloWorld3 - 3 error(s), 0 warning(s) 在“返回”之前1>构建日志保存在“文件:/ ... \\ Projects \\ HelloWorld3 \\ HelloWorld3 \\ Debug \\ BuildLog.htm” 1> HelloWorld3-3个错误,0个警告

#include <iostream>

int main()
{


cout <<"Hello World!<<endl;
return 0;
}

cout is in the namespace "std", so you have two options: cout在名称空间“ std”中,因此您有两个选择:

  1. prefix its use with std 使用std作为前缀

    std::cout << "Hello World" << std::endl; std :: cout <<“ Hello World” << std :: endl;

  2. declare that you are using the namespace std 声明您正在使用命名空间std

    using namespace std; 使用名称空间std;

1>...\\projects\\helloworld3\\helloworld3\\hello.cpp(7) : error C2065: 'cout' : undeclared identifier 1> ... \\ projects \\ helloworld3 \\ helloworld3 \\ hello.cpp(7):错误C2065:“ cout”:未声明的标识符

What this is saying is that it doesn't know what cout is. 这是说它不知道什么是cout。 In C++ names can be in namespaces. 在C ++中,名称可以位于名称空间中。 In the case of cout it is in the namespace std. 如果是cout,则位于名称空间std中。 You can tell the compiler to look there in 2 ways: 您可以通过两种方式告诉编译器:

  • with the line, using namespace std; using namespace std; this tells the compiler to bring in all the names in the namespace std into the current one. 这告诉编译器将名称空间std中的所有名称引入当前名称。
  • by using the scope resolution operator :: . 通过使用范围解析运算符:: as in std::cout Here you are telling the compiler exactly where to find the name. 就像在std::cout一样,您在这里告诉编译器确切的位置。

1>...\\projects\\helloworld3\\helloworld3\\hello.cpp(7) : error C2001: newline in constant 1> ... \\ projects \\ helloworld3 \\ helloworld3 \\ hello.cpp(7):错误C2001:常量中的换行符

This error says that the compiler is looking at a constant, in this case a string, and it found a newline where it didn't expect one. 该错误表明编译器正在查看一个常量(在本例中为字符串),并且在不需要的地方找到了换行符。 This is almost always a missing end quote. 这几乎总是缺少结尾的报价。

#include <iostream>

int main()
{
    std::cout << "Hello World!" << std::endl;
    return 0;
}

You need to use std::cout and std::endl rather than cout and endl , or do this after the #include : 您需要使用std::coutstd::endl而不是coutendl ,或者在#include之后执行此操作:

using namespace std;

The using clause makes your code more succinct, but in a large program it can be hard to keep track of where the names are coming from, so it can be better to be use the more verbose but more explicit std::cout / std::endl . using子句使您的代码更简洁,但是在大型程序中,可能很难跟踪名称的来源,因此最好使用更详细但更明确的std::cout / std::endl

You're also missing a closing quote here: 您还缺少此处的结束语:

cout <<"Hello World!<<endl;

You should have: 你应该有:

cout << "Hello World!" << endl;

ee1234. ee1234。 Seeing as this was likely your very first C++ program, go have a look at CPlusPlus.com it has a pretty straightforward and basic tutorial. 看到这很可能是您的第一个C ++程序,请访问CPlusPlus.com,它具有非常简单而基本的教程。 That is exactly where I started when I first jumped into c++. 这就是我第一次跳入c ++时的起点。 As far as good books go, well just do a search on SO for 'C++ Books' and you should have a plethora of good posts talking about appropriate beginner->advanced books. 就好书而言,只需在SO上搜索“ C ++书”就可以了,您应该有很多关于适当的初学者->高级书的好帖子。

Since cout is present within the standard namespace, you should either include 由于cout存在于标准名称空间中,因此您应该包括

using namespace std;

at the start of your code, under your includes, or use std:: in front of every function call. 在代码的开头,在您的include下,或在每个函数调用前使用std ::。 When placing an opening quote for a string, you should always include a closing quote aswell. 在为字符串放置开头引号时,还应始终包含结尾引号。 This results in 这导致

std::cout << "Hello World!" << std::endl;

Another way to write this would be: 另一种写法是:

std::cout << "Hello World!\n";

The \\n results in a new line feed. \\ n将导致换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM