Copy-Construktor und =Operator
-
Hallo Comunity!
Mein Code:
#include "complex.h" #include <iostream> Complex::Complex(double real_, double imag_) :real(real_) ,imag(imag_) {} double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } Complex& Complex::operator=(const Complex& a){ if (this != &a){ //Und hier stimmt auch was nicht?? this->real=a.get_real(); this->imag=a.get_imag(); return *this; } } std::ostream& operator<<(std::ostream& out, const Complex& a){ out<<"Realanteil: "<<a.get_real()<<std::endl; out<<"Imaginaerteil: "<<a.get_imag()<<std::endl; } Complex::Complex(const Complex& a){//Was muss da rein?}Hab schon ettliches probiert, aber ich komm auf keinen grünen Zweig!
Was muss im Zuweisungsoperator rein, und was im C-Ctor?Danke im Voraus
-
Complex Complex::operator=( const Complex& a ) { if( this != &a ) { this->real= a.real; this->imag = a.imag; return *this; } return *this; }So sollte eig. gehen.
MfG
Edit: In deinem Falle hätte der K-Konstruktor nicht das selbe der das = ? Aber hier bin ich mir nicht sicher. Wenn ich was falsches gesagt habe, entschuldige ich mich jetz schonmal

Complex::Complex(const Complex& a) { this->real = a.real; this->imag = a.imag; }
-
Genau das selbe hab ich schon probiert.
Doch bei mir kommen immer ergebnisse wie: 1.24344e+345 heraus.
-
Gib mal ein vollstaendiges Beispiel, dass den Fehler produziert.
Auf jeden Fall brauchst du hier weder Kopierkonstruktor noch Zuweisungsaoperator selber zu schreiben, da real und imag trivial kopierbar sind und daher die automatisch erzeugten Zuweisungsoperator, Kopierkonstruktor und Destruktor ihren Zweck perfekt erfuellen.
-
Complex my_Complex_1(11.2, 1.2345); Complex my_Complex_3(my_Complex_1); std::cout<<my_Complex_3<<std::endl;Das Ergebnis ist unbrauchbar:
Realanteil: 9.98919e-306
Imaginäranteil: 4.134e+234
-
Du hast SeppJ nicht verstanden. Wir wollen eine
mainsehen, wir wollen eine Definition aller Konstrukte sehen, die du in dermainbenutzt.
-
Ok
main:
#include <cstdlib> #include <iostream> #include "complex.h" // Funktionsprototypen ///////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// Complex operator+(const Complex&, const Complex&); Complex operator-(const Complex&, const Complex&); Complex operator*(const Complex&, const Complex&); Complex operator/(const Complex&, const Complex&); bool loop_control(); int main(int argc, char *argv[]) { Complex my_Complex_1(11.2, 1.2345); Complex my_Complex_2(1.1, 2.2); std::cout<<my_Complex_2<<std::endl; my_Complex_2=my_Complex_1; std::cout<<my_Complex_2<<std::endl; Complex my_Complex_3(my_Complex_1); std::cout<<my_Complex_3<<std::endl; system("Pause"); return 0; } // Funktionsdefinitionen /////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// Complex operator+(const Complex& a, const Complex& b){ Complex Complex_temp(a.get_real()+b.get_real(), a.get_imag()+b.get_imag()); return Complex_temp; } Complex operator-(const Complex& a, const Complex& b){ Complex Complex_temp(a.get_real()-b.get_real(), a.get_imag()-b.get_imag()); return Complex_temp; } Complex operator*(const Complex& a, const Complex& b){ Complex Complex_temp(a.get_real()*b.get_real(), a.get_imag()*b.get_imag()); return Complex_temp; } Complex operator/(const Complex& a, const Complex& b){ Complex Complex_temp(a.get_real()/b.get_real(), a.get_imag()/b.get_imag()); return Complex_temp; }Klasse:
class Complex{ double real; double imag; public: Complex(double real_, double imag_); double get_real() const; double get_imag() const; Complex& operator=(const Complex&); friend std::ostream& operator<<(std::ostream&, const Complex&); Complex(const Complex& a); };#include "complex.h" #include <iostream> Complex::Complex(double real_, double imag_) :real(real_) ,imag(imag_) {} double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } Complex& Complex::operator=(const Complex& a){ if (this != &a){ //Und hier stimmt auch was nicht?? this->real=a.get_real(); this->imag=a.get_imag(); return *this; } return *this; } std::ostream& operator<<(std::ostream& out, const Complex& a){ out<<"Realanteil: "<<a.get_real()<<std::endl; out<<"Imaginaerteil: "<<a.get_imag()<<std::endl; } Complex::Complex(const Complex& a){}
-
Dein Kopierkonstruktor ist leer. Ist doch klar, dass da nichts funktionierendes rauskommen wird. Ein moeglicher Kopierkonstruktor wurde dir schon genannt. Ausserdem wurdest du schon darauf hingewiesen, dass du sowohl den Zuweisungsoperator als auch den Kopierkonstruktor hier gar nicht brauchst. Lass die Deklaration und die Definition beider einfach weg.
-
Super jetzt funktionierts!

Danke