Complex class problem
Oct 6, 2010 at 7:23pm UTC
hi,
I'm making my own base c++ classes to use in couple of applications. Now, i'm working on my own Complex class for managing complex numbers, but there is something wrong. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
* main.cpp
*
* Created on: 6-okt-2010
* Author: Hannes
*/
#include "complex.h"
#include <iostream>
using namespace math;
int main( ) {
Complex a;
a.set_all( 1, 2 );
Complex b = a;
std::cout<<b.get_imag()<<std::endl;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
* complex.h
*
* Created on: 6-okt-2010
* Author: Hannes
*/
#ifndef COMPLEX_H_
#define COMPLEX_H_
namespace math {
class Complex {
private :
double re, im;
public :
Complex( double = 0, double = 0 );
Complex( const Complex& );
virtual ~Complex( void );
double get_real( void ); //return real part of complex number
double get_imag( void ); //return imaginary part of complex number
void set_all( double = 0, double = 0 ); //set real and imaginary part, default 0
void set_real( double = 0 ); //set real part of complex, default 0
void set_imag( double = 0 ); //set imaginary part of complex, default 0
};
};
#endif /* COMPLEX_H_ */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
* complex.cpp
*
* Created on: 6-okt-2010
* Author: Hannes
*/
#include "complex.h"
#include <iostream>
namespace math {
Complex::Complex( double real , double imag ) {
set_all( real, imag );
}
Complex::Complex( const Complex& c ){
set_all( c.re, c.im );
}
Complex::~Complex( void ) {
delete &re;
delete &im;
delete this ;
}
double Complex::get_imag( void ) {
return im;
}
double Complex::get_real( void ) {
return re;
}
void Complex::set_all( double real , double imag ){
re = real;
im = imag;
}
void Complex::set_real( double real ) {
re = real;
}
void Complex::set_imag( double imag ) {
im = imag;
}
}
There error comes up at the end, after printing the imaginary part of b.
hannes
Oct 6, 2010 at 8:10pm UTC
delete lines 21, 22, and 23. for that matter, delete the whole destructor; you don't need it.
Topic archived. No new replies allowed.