Getting an error stating that I cannot access a certain variable in my .ccp file
ID: 3553139 • Letter: G
Question
Getting an error stating that I cannot access a certain variable in my .ccp file of my class. Any idea what im doing wrong?
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
friend ostream &operator<<( ostream &, const Complex & );
friend istream &operator>>( istream &, Complex & );
public:
Complex(double = 0.0, double = 0.0);
Complex operator+(const Complex &) const;
Complex operator-(const Complex &) const;
Complex operator*(const Complex &) const;
bool operator==(const Complex &) const;
bool operator!=(const Complex &) const;
void print();
private:
double real;
double imaginary;
};
#endif
#include <iostream>
#include <string>
#include <iomanip>
#include "Complex.h"
using namespace std;
Complex::Complex(double realPart, double imaginaryPart)
:real(realPart),
imaginary(imaginaryPart)
{
}
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
}
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
}
Complex Complex::operator*(const Complex &operand2) const
{
return Complex((real*operand2.real)-(imaginary*operand2.imaginary),(real*operand2.imaginary)+(imaginary*operand2.real));
}
bool Complex::operator==(const Complex &operand2) const
{
if (real == operand2.real && imaginary == operand2.imaginary)
return true;
else
return false;
}
bool Complex::operator!=(const Complex &operand2) const
{
if (real == operand2.real && imaginary == operand2.imaginary)
return false;
else
return true;
}
void Complex::print()
{
cout << '(' << real << ", " << imaginary << ')';
}
ostream &operator<<( ostream &output, const Complex &c) //The error is here with c and d real and imaginary
{
output << "(" << c.real << ", " << c.imaginary << ")";
return output;
}
istream &operator>>( istream &input, Complex &d)
{
input.ignore ();
input >>d.real;
input.ignore();
input>>d.imaginary;
input.ignore();
return input;
}
Explanation / Answer
/* Successfully compiling. All errors corrected. Corrections are highlighted in bold */
/*
SAVE AS Complex.h
*/
#ifndef COMPLEX_H
#define COMPLEX_H
#include<sstream> //u were missing this line.
using namespace std; // u were missing this line.
class Complex
{
friend ostream &operator<<( ostream &, const Complex & );
friend istream &operator>>( istream &, Complex & );
public:
Complex(double = 0.0, double = 0.0);
Complex operator+(const Complex &) const;
Complex operator-(const Complex &) const;
Complex operator*(const Complex &) const;
bool operator==(const Complex &) const;
bool operator!=(const Complex &) const;
void print();
private:
double real;
double imaginary;
};
#endif
==============================================
/*
SAVE AS Complex.cpp
*/
#include <iostream>
#include <string>
#include <iomanip>
#include "Complex.h"
using namespace std;
Complex::Complex(double realPart, double imaginaryPart)
:real(realPart),
imaginary(imaginaryPart)
{
}
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
}
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
}
Complex Complex::operator*(const Complex &operand2) const
{
return Complex((real*operand2.real)-(imaginary*operand2.imaginary),(real*operand2.imaginary)+(imaginary*operand2.real));
}
bool Complex::operator==(const Complex &operand2) const
{
if (real == operand2.real && imaginary == operand2.imaginary)
return true;
else
return false;
}
bool Complex::operator!=(const Complex &operand2) const
{
if (real == operand2.real && imaginary == operand2.imaginary)
return false;
else
return true;
}
void Complex::print()
{
cout << '(' << real << ", " << imaginary << ')';
}
ostream &operator<<( ostream &output, const Complex &c) //The error is here with c and d real and imaginary
{
output << "(" << c.real << ", " << c.imaginary << ")";
return output;
}
istream &operator>>( istream &input, Complex &d)
{
input.ignore ();
input >>d.real;
input.ignore();
input>>d.imaginary;
input.ignore();
return input;
}
int main()
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.