1
I tried to create a complex class in a C++ namespace per exercise, but when I try to define some methods of my class, the compiler returns error.
My code:
Complex. h:
#ifndef COMPLEX_N_H
#define COMPLEX_N_H
#include <iostream>
#include <math.h>
using namespace std;
namespace complex_number{
class complex_N{
public:
virtual double real() const;
virtual double imag() const;
virtual double module() const;
virtual double arg() const;
virtual const complex_N operator +(const complex_N& z);
virtual const complex_N operator -(const complex_N& z);
virtual const complex_N operator *(const complex_N& z);
virtual const complex_N operator /(const complex_N& z);
virtual const bool operator ==(const complex_N& z);
ostream& operator <<(ostream& outputStream);
};
}
#endif // COMPLEX_H
complex_e. h:
#ifndef COMPLEX_E_H
#define COMPLEX_E_H
#include "complex.h"
namespace complex_number{
class complex_e : public complex_N {
public:
complex_e();
complex_e(double a);
complex_e(double a, double b);
double real() const;
double imag() const;
double module() const;
double arg() const;
complex_e conj() const;
const complex_e operator +(const complex_e& z);
const complex_e operator -(const complex_e& z);
const complex_e operator *(const complex_e& z);
const complex_e operator /(const complex_e& z);
const bool operator ==(const complex_e& z);
private:
double re_;
double im_;
};
}
#endif
complex_e.cpp:
#include "complex_e.h"
complex_number::complex_e::complex_e():re_(0), im_(0){
}
complex_number::complex_e::complex_e(double a):re_(a), im_(0){
}
complex_number::complex_e::complex_e(double a, double b):re_(a), im_(b){
}
double complex_number::complex_e::real(){
return re_;
}
double complex_number::complex_e::imag(){
return im_;
}
double complex_number::complex_e::module(){
return sqrt(re_*re_+im_*im_);
}
double complex_number::complex_e::arg(){
return atan(im_/re_);
}
complex_number::complex_e complex_number::complex_e::conj(){
return complex_number::complex_e(re_,-im_);
};
const complex_number::complex_e operator+(const complex_number::complex_e& z){
return(complex_number::complex_e(z.re_+re_, z.im_+im_));
}
const complex_number::complex_e operator-(const complex_number::complex_e& z){
return(complex_number::complex_e(re_-z.re_, im_-z.im_));
}
When I try to compile this code I get errors like:
Error 1: no declaration Matches 'double complex_number::complex_e:real()'
Error 2: no declaration Matches 'double complex_number::complex_e:imag()'
For all functions defined in complex_e.cpp, except constructors and operators.
Meanwhile, in the operators I set I get errors like:
error: 're_' was not declared in this Scope
error: 'im_' was not declared in this Scope
Whenre_ and im_ should be the private attributes of the object calling the constructors.
How was the cpp of
complex_N
?– Isac