-1

I am trying to make a class Complex ( complex numbers of form z=a+ib m where a= _re and b=_im) and I need to create and constructor with 2 parametres(double,double). The _re and _im are 2 double pointers , and I don't know how to point them to a value. Here is the code , I can't modify the Class because is a school project .

#include <iostream>
#include <cmath>
using namespace std;

class Complex {
private:
    double* _re;
    double* _im;
public:
    Complex();
    Complex(double, double);
    void Display();
};

Complex::Complex()
{
    _re = 0;
    _im = 0;
    cout << "Complex::Complex()" << endl;
}

Complex::Complex(double re, double im)
{
    double* _re = new double;
    _re = &re;
    double* _im = new double;
    _im = &im;
     cout << "Complex::Complex(double re,double im)" << endl;
}

void Complex::Display()
{
    int nr = 1;
    cout << "z" << nr << "= " << *_re << "+i* " << *_im << endl; nr++;

}

int main()
{
    Complex z1(2,3);
    z1.Display();
    return 0;

}
  • 1
    Why do you want pointers? – Holt Mar 11 '21 at 10:41
  • 2
    `_re = &re;` this will back fire very hard and very quickly. – Timo Mar 11 '21 at 10:42
  • 6
    You seem to have some misunderstanding about how pointers work, and the need for pointers. Your class doesn't need pointers at all. – Some programmer dude Mar 11 '21 at 10:42
  • @Holt Because our techers wants that ... – denisxoctavian Mar 11 '21 at 10:47
  • @Someprogrammerdude The probleme is that I didn't created the class , i just need to make the the functions for the class and test them. I watched like 2-3 pointers tutorials but still have no idea how to fix this :/ – denisxoctavian Mar 11 '21 at 10:48
  • @denisxoctavian Don't watch tutorials – read books. – molbdnilo Mar 11 '21 at 10:50
  • 2
    That class definition is *awful*. Your instructor should not be teaching C++ if they think that is OK. – Caleth Mar 11 '21 at 10:55
  • 1
    The code you're looking for is `_re = new double(re);`. Now read about pointers and member variables and scope in your book until you understand why. – molbdnilo Mar 11 '21 at 10:57
  • actually the class is heavily broken. I'd refuse to work with it unless it is fixed. Seriously. Read about the rule of 3/5, try to convince your tutor that their code is crap, this way all would profit – 463035818_is_not_an_ai Mar 11 '21 at 10:58
  • 1
    It's also *really unfortunate* that `_re = 0;` is valid syntax here – Caleth Mar 11 '21 at 11:03
  • Quite honestly, this is a terrible way to teach pointers and their usage. It just doesn't make any sense for a class like that. I recommend you invest in [some decent books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn C++ "properly". – Some programmer dude Mar 11 '21 at 11:30

1 Answers1

0

You should update your constructor as below:

Complex::Complex(double re, double im)
{
    _re = new double;
    *_re = re;
    _im = new double;
    *_im = im;
    cout << "Complex::Complex(double re,double im)" << endl;
}

note that previously when you declared double* _re = new double it was a local variable. So you never initialized the class variables _re and _im.

Better constructor definition would be to use constructor member initializer list syntax. See below:

Complex::Complex(double re, double im)
    : _re(new double), _im(new double)
{
    *_re = re;
    *_im = im;
     cout << "Complex::Complex(double re,double im)" << endl;
}
Mital Vora
  • 2,199
  • 16
  • 19