×

Search anything:

cout is not a member of std [FIXED]

C++

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

In this article, we have explored the reason behind the error "cout is not a member of std" and 2 fixes to resolve this compilation error with C++ code.

Table of contents:

  1. Reason for the error
  2. Fix 1: Add header file
  3. Fix 2: For Microsoft's compiler MSVC

Reason for the error

While compiling a C++ code, you may face the following error:

cout is not a member of std

The reason is that the relevant header files are not provided in the code due to which the compiler is unable to locate the function cout.

Following C++ code which give this error when compiled:

int main() {
    std::cout << "data" << std::endl;
    return 0;
}

In this article at OpenGenus, we have presented fixes which will resolve this issue.

Fix 1: Add header file

The cout function is available in iostream header file which needs to be included at the beginning of the C++ code.

#include <iostream>

Following is the working code:

#include <iostream>

int main() {
    std::cout << "data" << std::endl;
    return 0;
}

Fix 2: For Microsoft's compiler MSVC

If you are compiling C++ code on Windows using Microsoft's compiler MSVC (Microsoft Visual Code), include the header file stdafx.h just before iostream.

stdafx.h is a precompiled header file which is used in Microsoft Visual Studio to keep track of files which have been compiled once and not changed following it. This is an optimization in MSVC to reduce compilation time. If it is not added, correct header files are not included in subsequent runs. If iostream header file is not included due to this issue, the compilation error will come.

Include these header files in the same order:

#include "stdafx.h"
#include <iostream>

Following is the working code:

#include "stdafx.h"
#include <iostream>

int main() {
    std::cout << "data" << std::endl;
    return 0;
}

Compile using MSVC.

With these fixes in this article at OpenGenus, the error must be resolved. Continue with your development.

Geoffrey Ziskovin

Geoffrey Ziskovin

Geoffrey Ziskovin is an American Software Developer and Author with an experience of over 30 years. He started his career with Haskell and has interviewed over 700 candidates for Fortune 500 companies

Read More

Improved & Reviewed by:


OpenGenus Foundation OpenGenus Foundation
cout is not a member of std [FIXED]
Share this