C++ cout 不是 std 的成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11375949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:05:49  来源:igfitidea点击:

cout is not a member of std

c++iostdmembercout

提问by Paul Hannon

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

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

添加.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:

错误显示在 io.cpp 中。确切的错误是:

enter image description here

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.

编辑:我昨天用相同数量的文件(2 .cpp 和 1.h)做了一个小项目,我没有在另一个 .cpp 中包含 iostream 头文件,它仍然编译并运行良好。

回答by unkulunkulu

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

添加#include <iostream>io.cpp太开始。

回答by Jukes

Also remember that it must be:

还要记住,它必须是:

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

and not the other way around

而不是相反

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

回答by Marco Rubio

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

我有一个类似的问题,结果我不得不添加一个额外的条目cmake来包含这些文件。

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

由于我也在使用 zmq 库,因此我也必须将其添加到包含的库中。