 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rolf Magnus Guest
|
Posted: Mon Apr 11, 2005 10:51 pm Post subject: Re: Komplexe Zahlen - warum geht das nicht? |
|
|
Ottmar Ohlemacher wrote:
| Quote: | Hi,
Warum geht das nicht?
Folgendes Program sei gegeben:
#include<iostream
#include
#include
using namespace std;
int main()
{
complex
a1 = a1 + (2.2, 3.1);
|
Weil (2.2, 3.1) kein Objekt der Klasse complex<float> ist. Stattdessen ist
es äquivalent zu 3.1.
Versuch mal:
a1 = a1 + complex<float>(2.2, 3.1);
| Quote: | cout << a1;
getchar();
}
|
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Ozan Ayyüce Guest
|
Posted: Mon Apr 11, 2005 11:34 pm Post subject: Re: Komplexe Zahlen - warum geht das nicht? |
|
|
Ottmar Ohlemacher:
| Quote: | a1 = a1 + (2.2, 3.1);
|
Du weist dort a1 die Summe von a1 und dem Rückgabewert des ,-operators
zu. Dieser ist immer der Wert hinter dem ',', hier also 3.1. Obige Zeile
ist also äquivalent zu a1 = a1 + 3.1;
Die Klasse complex hat einen +-operator, der als Argument einen Wert vom
typ float akzeptiert. So wird kein Objekt des Typs complex benötigt und
es entsteht kein Fehler, das Ergebnis ist jedoch nicht wie von dir
erwartet.
Du musst also den compiler ausdrücklich anweisen, ein Objekt des Typs
complex zu erzeugen. Um dies innerhalb eines Ausdrucks zu tun brauchst
du nur den contructor mit passenden Argumenten aufzurufen:
a1 = a1 + complex<float>(2.2, 3.1);
Ozan
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|