cout is not a member of std

C++IoStdMemberCout

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

Also remember that it must be:

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

and not the other way around

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

Solution 3 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul HannonView Question on Stackoverflow
Solution 1 - C++unkulunkuluView Answer on Stackoverflow
Solution 2 - C++JukesView Answer on Stackoverflow
Solution 3 - C++Marco RubioView Answer on Stackoverflow