简体   繁体   中英

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

I am trying to cout some variables but the compiler says that cout is undefined . I have included iostream and am using namespace std. Removing using namespace std and using std::cout instead changes the issue to "namespace "std" has no member "cout" ". I found some answers saying to add # include "stdafx.h" to the code but Error: cannot open source file "stdafx.h" occurs.

Code is:

#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;
    }
};

I'm trying to define a class, so my main is elsewhere. Running a very basic program that just couts "hello world" works fine, the problem is specific to this code.

Put #include<iostream> at the first position, the order is important

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

PS: Why do you use std:: when you are using "using namespace std;"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM