Eu tentei criar uma classe de complexos em um namespace do C++ por exercício, mas quando tento definir alguns métodos da minha classe, o compilador retorna erro.
Meu código:
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_));
}
Quando eu tento compilar esse código eu recebo erros como:
Erro 1: no declaration matches 'double complex_number::complex_e::real()'
Erro 2: no declaration matches 'double complex_number::complex_e::imag()'
Para todas as funções definidas no complex_e.cpp, exceto os construtores e operadores.
Enquanto isso, nos operadores que eu defini eu recebo erros como:
error: 're_' was not declared in this scope
error: 'im_' was not declared in this scope
Quando re_ e im_ deveriam ser os atributos privados do objeto que chama os construtores.
complex_N
?complex_e.cpp
deve usar o mesmonamespace
. É para isso que existe.complex_number
só existe nessenamespace
, mas poderia existir em vários. E se declara um métodoconst
no header deve declarar igual nocpp
. Isso é overloading, podem existir versõesconst
e nãoconst
e o compilador não vai saber qual está faltando, já que vai esperar por 2 e 2 no header e no cpp