Ошибка no member named cout in namespace std

I’m practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple.

Here are my files:

main.cpp

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

int main()
{
    int x = readNumber();
    int y = readNumber();

    writeAnswer(x + y);

    return(0);
}

io.cpp

int readNumber()
{
    int x;

    std::cout << "Number: ";
    std::cin >> x;

    return x;
}

void writeAnswer(int x)
{
    std::cout << "Answer: ";
    std::cout << x;
}

add.h

#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

int readNumber();
void writeAnswer(int x);

#endif // #ifndef ADD_H_INCLUDED

The error is showing up in io.cpp. The exact errors are:

enter image description here

Does anyone have any idea why this may be happening? Thanks.

EDIT: I made a small project yesterday with the same amount of files (2 .cpp and 1.h) and I didn’t include the iostream header in the other .cpp and it still compiled and ran fine.

Ernestas Gruodis's user avatar

asked Jul 7, 2012 at 14:43

Paul Hannon's user avatar

3

add #include <iostream> to the start of io.cpp too.

answered Jul 7, 2012 at 14:45

unkulunkulu's user avatar

unkulunkuluunkulunkulu

11.5k2 gold badges31 silver badges49 bronze badges

5

If you’re using pre-compiled headers with Microsoft’s compiler (MSVC), remember that it must be:

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

and not the other way around:

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

In other words, the pre-compiled header include file must always come first. (The compiler should give you an error specifically explaining this if you forget.)

Cody Gray - on strike's user avatar

answered Sep 20, 2018 at 19:41

Jukes's user avatar

JukesJukes

3973 silver badges5 bronze badges

0

I had a similar issue and it turned out that i had to add an extra entry in cmake to include the files.

Since i was also using the zmq library I had to add this to the included libraries as well.

answered May 22, 2018 at 17:26

Marco Rubio's user avatar

Dimedron

0 / 0 / 0

Регистрация: 09.09.2015

Сообщений: 1

1

06.06.2017, 21:00. Показов 36613. Ответов 1

Метки нет (Все метки)


Студворк — интернет-сервис помощи студентам

Вот такие ошибки в общем

Ошибка namespace "std" не содержит члена "cout"

Собственно, вот сам код

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
 
#include <Windows.h>
#include <fstream>
#pragma warning(disable: 4996)
 
 
void LogicalDrive()
{
    FILE* file = fopen("C:\Users\GLaDOS\Documents\Visual Studio 2017\ProjectslogicalDisk.txt", "w");
    char buf[26];
    GetLogicalDriveStringsA(sizeof(buf), buf);
    char *DRF[] = { "Unknown" , "Invalid path", "Removable", "Fixed" , "Network drive","CD-ROM", "RAM disk" };
    for (char *s = buf; *s; s += strlen(s) + 1)
    {
        std::cout << s << "  " << DRF[GetDriveTypeA(s)] << std::endl;
        fputs(s, file);
        fputs(DRF[GetDriveTypeA(s)], file);
        fputs("n", file);
    }
    fclose(file);
}



0



Programming

Эксперт

94731 / 64177 / 26122

Регистрация: 12.04.2006

Сообщений: 116,782

06.06.2017, 21:00

Ответы с готовыми решениями:

error C2886: std::cout: использование символа в «using»-объявлении члена не допускается
подскажите плз что ето может бить…если не подключаю файл Nokia.h тогда всьо норм..
#include…

Где ошибка невозможно преобразовать указатель «this» из «const pers» в «pers &» при выводе объектов через cout
Добрый день!
Переписал код из книг Лафоре, создание мультимножества из собственных объектов-…

В зависимости от времени года «весна», «лето», «осень», «зима» определить погоду «тепло», «жарко», «холодно», «очень холодно»
В зависимости от времени года &quot;весна&quot;, &quot;лето&quot;, &quot;осень&quot;, &quot;зима&quot; определить погоду &quot;тепло&quot;,…

namespace «std» has no member «vector»
Собственно такая проблема, мне нужно использовать в программе std::vector, однако VC ругается, мол…

1

  • VsCode Version: 1.23.1
  • Extension Version: 0.17.1
  • Os: Win10 Pro Education
  • Version: 1803
  • gcc : wsl ubuntu16.04(5.4)

I’ve followed instruction described in https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/LanguageServer/Windows%20Subsystem%20for%20Linux.md, however, the intellisense tool generate: namespace «std» has no member «cout». This issue also is presented with other members in headers I use in my project, e.g., namespace «omnetpp» has no member «simsignal_t»

c_cpp_properties.json

{
    "configurations": [
        {

            "name": "WSL",
            "intelliSenseMode": "clang-x64",
            "compilerPath": "/usr/bin/gcc",
            "includePath": [
                "${workspaceFolder}",
                "/mnt/c/Users/Geo/omnetpp-5.2/include",
                "/mnt/c/Users/Geo/omnetpp-5.2/include/omnetpp",
                "/mnt/c/Users/Geo/omnetpp-5.2/include/omnetpp/platdep",
                "/mnt/c/Users/Geo/omnetpp-5.2/include/platdep",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/cmdenv",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/common",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/envir",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/eventlog",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/layout",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/nedxml",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/qtenv",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/qtenv/osg",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/scave",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/sim",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/sim/netbuilder",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/sim/parsim",
                "/mnt/c/Users/Geo/omnetpp-5.2/src/tkenv",
                "/mnt/c/Users/Geo/inet/src",
                "/mnt/c/Users/Geo/inet/src/inet",
                "/mnt/c/Users/Geo/inet/src/inet/common",
                "/mnt/c/Users/Geo/inet/src/inet/common/geometry/common",
                "/mnt/c/Users/Geo/inet/src/inet/mobility/base",
                "/mnt/c/Users/Geo/inet/src/inet/mobility/contract",
                "/mnt/c/Users/Geo/inet/src/inet/mobility/single",
                "${workspaceRoot}/src",
                "${workspaceRoot}/src/base",
                "${workspaceRoot}/src/engine",
                "${workspaceRoot}/src/simple"
            ],
            "defines": [
            ],
            "browse": {
                "path": [
                    "${workspaceFolder}",
                    "/mnt/c/Users/Geo/omnetpp-5.2/include",
                    "/mnt/c/Users/Geo/omnetpp-5.2/src",
                    "/mnt/c/Users/Geo/inet/src",
                    "/mnt/c/Users/Geo/gsim/slaw/src"
        
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

ive made a simple hello world script:

#include <QCoreApplication>
#include <iostream>

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

I get an error in the editor, and not when running the script. the thing is, i dont get why the editor tells me something is wrong, and i dont really know how to fix it, maybe someone else have runned into the same issue?
cout-error.png
terminal-output.png

Edit:
Thanks everyone for helping me!
mrjj’s solution: Turning off «clang format» in help > About_Plugins seems to work (in hiding every clang notification thus hiding even the incorrect ones). Im fine with the solution though, they arent useful in my opinion anyway.

Learn Algorithms and become a National Programmer

Indian Technical Authorship Contest starts on 1st July 2023. Stay tuned.

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.

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.

Понравилась статья? Поделить с друзьями:
  • Ошибка no newline at end of file
  • Ошибка no matching function for call to qt
  • Ошибка no more mirrors to try
  • Ошибка no matching distribution found for
  • Ошибка no module named pandas