ee1234
ee1234

Reputation: 21

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

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 ';' before 'return' 1>Build log was saved at "file:/...\Projects\HelloWorld3\HelloWorld3\Debug\BuildLog.htm" 1>HelloWorld3 - 3 error(s), 0 warning(s)

#include <iostream>

int main()
{


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

Upvotes: 1

Views: 1841

Answers (5)

Matt Price
Matt Price

Reputation: 45345

1>...\projects\helloworld3\helloworld3\hello.cpp(7) : error C2065: 'cout' : undeclared identifier

What this is saying is that it doesn't know what cout is. In C++ names can be in namespaces. In the case of cout it is in the namespace std. You can tell the compiler to look there in 2 ways:

  • with the line, using namespace std; this tells the compiler to bring in all the names in the namespace std into the current one.
  • by using the scope resolution operator ::. as in std::cout Here you are telling the compiler exactly where to find the name.

1>...\projects\helloworld3\helloworld3\hello.cpp(7) : error C2001: newline in constant

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;
}

Upvotes: 3

DeusAduro
DeusAduro

Reputation: 6066

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. That is exactly where I started when I first jumped into 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.

Upvotes: 2

Charles
Charles

Reputation: 2661

Since cout is present within the standard namespace, you should either include

using namespace std;

at the start of your code, under your includes, or use std:: in front of every function call. 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.

Upvotes: 1

waterlooalex
waterlooalex

Reputation: 13892

cout is in the namespace "std", so you have two options:

  1. prefix its use with std

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

  2. declare that you are using the namespace std

    using namespace std;

Upvotes: 3

RichieHindle
RichieHindle

Reputation: 281475

You need to use std::cout and std::endl rather than cout and endl, or do this after the #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.

You're also missing a closing quote here:

cout <<"Hello World!<<endl;

You should have:

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

Upvotes: 2

Related Questions