3

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.

1 Answer 1

4

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;"?

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, forgot to edit that out. I've tried using one or the other separately but neither works
Ty to put #include<iostream> at the first position, maybe the order is important
Actually, #include "Complex.h" should be the first line of "Complex.cpp"; The error is not putting the includes the header needs inside the header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.