【问题标题】:Error: Identifier "cout" is undefined. <iostream> included and using namespace std;错误:标识符“cout”未定义。 <iostream> 包含并使用命名空间 std;
【发布时间】:2015-05-21 02:48:30
【问题描述】:

我正在尝试cout 一些变量,但编译器说cout is undefined。我已经包含了 iostream 并且正在使用命名空间 std。删除 using namespace stdusing std::cout 会将问题改为“命名空间“std”没有成员“cout””。我发现一些答案说要在代码中添加# include "stdafx.h",但会出现Error: cannot open source file "stdafx.h"

代码是:

#include "Complex.h"
#include <cmath>
#include <iostream>

using namespace std;

Complex::Complex(int PolarOrRectang, float RealOrArg, float ImagOrAng) {
    if (PolarOrRectang == 0) {
        real = RealOrArg;
        imag = ImagOrAng;
    else {
        real = RealOrArg * cos(ImagOrAng);
        imag = RealOrArg * sin(ImagOrAng);
    }
};

void Complex::getValue(int PolarOrRectang) {
    if (PolarOrRectang == 0) {
        cout << real << " +_" << imag << "i" << endl;
    } else {
        cout << sqrt((real^2) + (imag^2)) << "*e^-" << atan(imag / real)<< endl;
    }
};

我正在尝试定义一个类,所以我的主要在别处。 运行一个非常基本的程序,只输出“hello world”就可以了,问题是这段代码特有的。

【问题讨论】:

    标签: c++ undefined cout


    【解决方案1】:

    #include&lt;iostream&gt;放在首位,顺序很重要

    #include "Complex.h"
    #include <iostream>
    #include <cmath>
    

    PS:为什么在使用“using namespace std;”的时候还要使用std::?

    【讨论】:

    • 抱歉,忘记编辑了。我试过单独使用一个或另一个,但都不起作用
    • 把#include放在第一位,可能顺序很重要
    • 其实#include "Complex.h"应该"Complex.cpp"的第一行;错误是没有把包含的标头需要 inside 标头。
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2014-11-23
    • 2018-05-07
    • 2018-11-27
    • 2022-01-11
    相关资源
    最近更新 更多