Hello! I am writing the first program on c ++ and I don’t understand why the error is displayed:

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

Here is the code:

#include <iostream> int main() { cout << "Hello world" << endl; //тут ошибка } 

Error in line number 3.
Tell me, please, what could be the problem?

  • four
    std :: cout obviously - igumnov
  • 2
    Or add before main using namespace std; - avp 5:03 pm
  • Thanks to everyone - block
  • 2
    And it would be nice to return something from this function. Well, just in case) - brightside90

1 answer 1

You need to define the std . All C ++ standard library identifiers are defined in a namespace with that name. The namespace defines the scope of identifiers. You can determine directly when “Hello world” is displayed (this entry is called the direct identifier refinement):

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

There is also another way: you can write before the main() function:

 using namespace std; 

This entry is used most often (for simplicity, in short listings). It means that after its execution all identifiers of the std space are available as if they were declared globally. However, do not abuse this record, because in complex programs it can lead to periodic name conflicts, unpredictable consequences due to rules of overload.

Another option is to use a certain namespace. The example shows how to get a local opportunity to skip the prefix std:: for cout and endl :

 using std::cout; using std::endl;