Important update: Arm Announces End of Life Timeline for Mbed. This site will be archived in July 2026. Read the full announcement.
Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
Get the error message "class complex has no suitable copy constructor"
Hi, the code isn´t realy readable. Here is it once more:
----Code-------
- include "mbed.h"
- include <stdint.h>
------
Definition der Klasse Complex um Komplexe Zahlen fuer diverse
Aufgaben, wie z.B. FFT, verarbeiten zu koennen
class complex { private: float real; Real Part float imag; Imaginary Part
public: complex(float,float); complex(complex&); complex operator +(complex); complex operator -(complex); complex operator *(complex); complex operator /(complex); complex getkonjugierte(); complex getreziprok(); float getbetrag(); void setdata(float,float); void setreal(float); void setimag(float); float getreal(); float getimag(); bool operator ==(complex); void operator =(complex); };
CONSTRUCTOR complex::complex(float r=0.0f,float im=0.0f) { real=r; imag=im; }
COPY CONSTRUCTOR complex::complex(complex& c) { this->real=c.real; this->imag=c.imag; }
Ueberladen des Operators = fuer komplexe Zahlen void complex::operator =(complex c) { this->real=c.real; this->imag=c.imag; }
Ueberladen des Operators + fuer komplexe Zahlen complex complex::operator +(complex c) { complex tmp; tmp.real=this->real+c.real; tmp.imag=this->imag+c.imag; return tmp; }
Ueberladen des Operators - fuer komplexe Zahlen complex complex::operator -(complex c) { complex tmp; tmp.real=this->real - c.real; tmp.imag=this->imag - c.imag; return tmp; }
Ueberladen des Operators * fuer komplexe Zahlen complex complex::operator *(complex c) { complex tmp; tmp.real=(real*c.real)-(imag*c.imag); tmp.imag=(real*c.imag)+(imag*c.real); return tmp; }
Ueberladen des Operators / fuer komplexe Zahlen complex complex::operator /(complex c) { float div=(c.real*c.real) + (c.imag*c.imag); complex tmp; tmp.real=(real*c.real)+(imag*c.imag); tmp.real/=div; tmp.imag=(imag*c.real)-(real*c.imag); tmp.imag/=div; return tmp; }
Berechne die Konjugierte der komplexen Zahl complex complex::getkonjugierte() { complex tmp; tmp.real=this->real; tmp.imag=this->imag * -1; return tmp; }
Berechne den reziproken Wert der komplexen Zahl (1/X) complex complex::getreziprok() { complex t; t.real=real; t.imag=imag * -1; float div; div=(real*real)+(imag*imag); t.real/=div; t.imag/=div; return t; }
Berechne den Betrag der komplexen Zahl sqrt(real^2+imag^2) float complex::getbetrag() { float z; z=(real*real)+(imag*imag); z=sqrt(z); return z; }
Setzen der komplexen Zahl void complex::setdata(float r,float i) { real=r; imag=i; }
Setzen des Realwertes der komplexen Zahl void complex::setreal(float r) { real=r; }
Setzen des Imaginaerwertes der komplexen Zahl void complex::setimag(float i) { imag=i; }
Abfragen des Realanteils der komplexen Zahl float complex::getreal() { return real; }
Abfragen des Imaginaeranteils der komplexen Zahl float complex::getimag() { return imag; }
Ueberladen des Operators == bei logischen Vergleichsabfragen bool complex::operator ==(complex c) { return (real==c.real)&&(imag==c.imag) ? 1 : 0; }
Ende der Klasse complex
--------
BusOut myled(LED1, LED2, LED3, LED4);
complex Zahl1(2.0f, 1.0f); complex Zahl2(3.0f, 2.0f); complex Zahl3(4.0f, 3.0f);
int main() { while(1) { Zahl3 = Zahl1 + Zahl1;
myled = Zahl3.getreal(); wait(2.0); myled = Zahl3.getimag(); wait(2.0); } }
Hi Fellows, I have a small problem with my copy constructor definition in the class complex, which I have directly copied from cplusplus.com into my project. I want to define complex datatypes in order to implement FFT for some lessons in our technical highschool. The expression Zahl1 = Zahl2 (both are of type complex) produces no error, but Zahl1 = Zahl2 + Zahl3 (all are of type complex) produces the compiler error "class complex has no suitable copy constructor". What could be wrong? I would be glad if somebody can give me a hint.
Best regards Roland
Here is the complete code:
---begin---------------class complex { private: float real; Real Part float imag; Imaginary Part
public: complex(float,float); complex(complex&); complex operator +(complex); complex operator -(complex); complex operator *(complex); complex operator /(complex); complex getkonjugierte(); complex getreziprok(); float getbetrag(); void setdata(float,float); void setreal(float); void setimag(float); float getreal(); float getimag(); bool operator ==(complex); void operator =(complex); };
CONSTRUCTOR complex::complex(float r=0.0f,float im=0.0f) { real=r; imag=im; }
COPY CONSTRUCTOR complex::complex(complex& c) { this->real=c.real; this->imag=c.imag; }
Ueberladen des Operators = fuer komplexe Zahlen void complex::operator =(complex c) { this->real=c.real; this->imag=c.imag; }
Ueberladen des Operators + fuer komplexe Zahlen complex complex::operator +(complex c) { complex tmp; tmp.real=this->real+c.real; tmp.imag=this->imag+c.imag; return tmp; }
Ueberladen des Operators - fuer komplexe Zahlen complex complex::operator -(complex c) { complex tmp; tmp.real=this->real - c.real; tmp.imag=this->imag - c.imag; return tmp; }
Ueberladen des Operators * fuer komplexe Zahlen complex complex::operator *(complex c) { complex tmp; tmp.real=(real*c.real)-(imag*c.imag); tmp.imag=(real*c.imag)+(imag*c.real); return tmp; }
Ueberladen des Operators / fuer komplexe Zahlen complex complex::operator /(complex c) { float div=(c.real*c.real) + (c.imag*c.imag); complex tmp; tmp.real=(real*c.real)+(imag*c.imag); tmp.real/=div; tmp.imag=(imag*c.real)-(real*c.imag); tmp.imag/=div; return tmp; }
Berechne die Konjugierte der komplexen Zahl complex complex::getkonjugierte() { complex tmp; tmp.real=this->real; tmp.imag=this->imag * -1; return tmp; }
Berechne den reziproken Wert der komplexen Zahl (1/X) complex complex::getreziprok() { complex t; t.real=real; t.imag=imag * -1; float div; div=(real*real)+(imag*imag); t.real/=div; t.imag/=div; return t; }
Berechne den Betrag der komplexen Zahl sqrt(real^2+imag^2) float complex::getbetrag() { float z; z=(real*real)+(imag*imag); z=sqrt(z); return z; }
Setzen der komplexen Zahl void complex::setdata(float r,float i) { real=r; imag=i; }
Setzen des Realwertes der komplexen Zahl void complex::setreal(float r) { real=r; }
Setzen des Imaginaerwertes der komplexen Zahl void complex::setimag(float i) { imag=i; }
Abfragen des Realanteils der komplexen Zahl float complex::getreal() { return real; }
Abfragen des Imaginaeranteils der komplexen Zahl float complex::getimag() { return imag; }
Ueberladen des Operators == bei logischen Vergleichsabfragen bool complex::operator ==(complex c) { return (real==c.real)&&(imag==c.imag) ? 1 : 0; }
Ende der Klasse complex
--------BusOut myled(LED1, LED2, LED3, LED4);
complex Zahl1(2.0f, 1.0f); complex Zahl2(3.0f, 2.0f); complex Zahl3(4.0f, 3.0f);
int main() { while(1) { Zahl3 = Zahl1 + Zahl1;
myled = Zahl3.getreal(); wait(2.0); myled = Zahl3.getimag(); wait(2.0); } }
----end---------