错误:标识符“ cout”未定义。包含<iostream>并使用命名空间std;

Buc*_*val 3 c++ cout undefined

我正在尝试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;
    }
};
Run Code Online (Sandbox Code Playgroud)

我正在尝试定义一个类,所以我的主要课程在其他地方。运行一个仅会提示“ hello world”的非常基本的程序即可正常工作,该问题特定于此代码。

Bla*_*ack 5

#include<iostream>在第一位置,顺序很重要

#include "Complex.h"
#include <iostream>
#include <cmath>
Run Code Online (Sandbox Code Playgroud)

PS:为什么在使用“使用命名空间std;”时使用std ::?

  • 实际上,“#include“ Complex.h”`*应该*是“ Complex.cpp”`的第一行;错误不是将头文件包含在头文件中。 (2认同)