coutidentifierendl

cout and endl are undefined even though using std:: and <iostream>


I am trying to enter the simple "Hello World!" code, but cout and endl are undefined.

#include<iostream>
#include "stdafx.h"
int main()
{
    std::cout << "Hello World!" << std::endl;
}

It turns out the errors: "'cout': is not a member of 'std', note: see declaration of 'std', 'cout': undeclared identifier", and the same with endl. Please Help.


Solution

  • You are missing a space in the #include statement: #include<iostream>

    This is the correct way: #include <iostream>

    Full code:

    #include <iostream>
    #include "stdafx.h"
    int main()
    {
        std::cout << "Hello World!" << std::endl;
    }