错误:标识符&#34; cout&#34;未定义。 <iostream>包含并使用namespace std; </iostream>

时间:2015-03-18 10:49:35

标签: c++ undefined cout

我正在尝试cout一些变量,但编译器说cout is undefined。我已经包含了iostream并使用了命名空间std。删除using namespace stdusing std::cout会将问题更改为&#34; namespace&#34; std&#34;没有会员&#34; cout&#34; &#34 ;.我找到了一些答案,说明要在代码中添加# 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;
    }
};

我试图定义一个类,所以我的主要是在其他地方。 运行一个非常基本的程序,只是cout&#34; hello world&#34;工作正常,问题是特定于此代码。

1 个答案:

答案 0 :(得分:4)

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

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

PS:当你使用“using namespace std;”时,为什么要使用std ::?