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

Error: Identifier "cout" is undefined. <iostream> included and using namespace std;

本文关键字:包含 std 命名空间 gt lt 标识符 cout 未定义 错误 iostream      更新时间:2023-10-16

我正在尝试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;
    }
};

我正在尝试定义一个类,所以我的main在别处。运行一个只定制"helloworld"的非常基本的程序可以很好地工作,问题是这个代码特有的。

#include<iostream>放在第一个位置,订单是重要的

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

PS:当你使用"使用命名空间std;"时,为什么要使用std:?