简体   繁体   中英

Error: 'cout' : undeclared identifier; though I've included iostream header file in program

I am trying to compile the simple program below. But, it's not compiling & gives error:

error C2065: 'cout' : undeclared identifier

I want to ask you that why this program doesn't work though I've included iostream header file in it?

#include <iostream>

void function(int) { cout << “function(int) called” << endl; }
void function(unsigned int) { cout << “function(unsigned int) called” << endl; }
    int main()
    {
        function(-2);
        function(4);
        return 0;
    }

Thanks in advance.

The cout stream is defined in the std namespace. So to name it you write:

std::cout

If you want to shorten this to cout then you can write

using namespace std;

or

using std::cout;

before writing cout.

Any good documentation source will tell you which namespace contains an object. For instance: http://en.cppreference.com/w/cpp/io/cout

你必须写std::coutusing std;添加using std;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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