Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#ifndef VEKTOR3D #define VEKTOR3D class Vektor3D { private: double x,y,z; public: Vektor3D(); Vektor3D(double x, double y, double z); ~Vektor3D(); double GetX(); double GetY(); double GetZ(); void SetX(double x); void SetY(double y); void SetZ(double z); Vektor3D operator+(Vektor3D vec); }; #endif |
C-/C++-Quelltext |
|
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 48 49 |
#include "stdafx.h" #include "Vektor3D.h" Vektor3D::Vektor3D(){ x = 0; y = 0; z = 0; } Vektor3D::Vektor3D(double x, double y, double z){ this->x = x; this->y = y; this->z = z; } double Vektor3D::GetX(){ return x; } double Vektor3D::GetY(){ return y; } double Vektor3D::GetZ(){ return z; } void Vektor3D::SetX(double x){ this->x = x; } void Vektor3D::SetY(double y){ this->y = y; } void Vektor3D::SetZ(double z){ this->z = z; } Vektor3D Vektor3D::operator+(Vektor3D vec){ x += vec.GetX(); y += vec.GetY(); z += vec.GetZ(); return *this; } Vektor3D::~Vektor3D(){ } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "stdafx.h" #include <iostream> #include "Vektor3D.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Vektor3D *v1 = new Vektor3D(); Vektor3D *v2 = new Vektor3D(1.0, 2.0, 3.0); Vektor3D *v3 = new Vektor3D(1.0, 1.0, 1.0); v1 = v2 + v3; // <-- Hier is v3 rot unterstrichen und mir wird gesagt... "Error: der Ausdruck muss einen Integralen Typ oder einen Enumerationstyp aufweisen". system("Pause"); return 0; } |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »vanillebaerchen« (09.02.2011, 00:20)
C-/C++-Quelltext |
|
1 |
*v1 = *v2 + *v3; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
Vektor3D& operator +=(const Vektor3D& vec) // Memberfunktion { x += vec.GetX(); y += vec.GetY(); z += vec.GetZ(); return *this; } Vektor3D operator +(const Vektor3D& a, const Vektor3D& a) // keine Memberfunktion { return Vektor3D(a.GetX() + b.GetX(), a.GetY() + b.GetY(), a.GetZ() + b.GetZ()); } |
Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von »dot« (08.02.2011, 13:45)
Und das mit den gettern und settern habe ich mir aus Java von meinem Prof. angewöhnt, der meint immer alles was geht kapseln.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#ifndef VEKTOR3D #define VEKTOR3D class Vektor3D { public: double x,y,z; Vektor3D(); Vektor3D(double x, double y, double z); ~Vektor3D(); }; #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include "stdafx.h" #include "Vektor3D.h" Vektor3D::Vektor3D(){ x = 0; y = 0; z = 0; } Vektor3D::Vektor3D(double x, double y, double z){ this->x = x; this->y = y; this->z = z; } Vektor3D::~Vektor3D(){ } Vektor3D operator +(const Vektor3D left, const Vektor3D right) { return Vektor3D(left.x + right.x, left.y + right.y, left.z + right.z); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include "stdafx.h" #include <iostream> #include "Vektor3D.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Vektor3D *v1 = new Vektor3D(); Vektor3D *v2 = new Vektor3D(1.0, 2.0, 3.0); Vektor3D *v3 = new Vektor3D(1.0, 1.0, 1.0); *v1 = *v2 + *v3; system("Pause"); return 0; } |
ok dann will ich mich mal belehren lassen .
jetzt meckert der aber rum, dass kein operand dafür vorhanden ist.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 |
Vektor3D::Vektor3D() : x(0.0), y(0.0), z(0.0) { } Vektor3D::Vektor3D(double x, double y, double z) : x(x), y(y), z(z) { } |
C-/C++-Quelltext |
|
1 2 3 4 |
Vektor3D operator +(Vektor3D& left, Vektor3D& right) { return Vektor3D(left.x + right.x, left.y + right.y, left.z + right.z); } |
da ich es jetzt doch als referenz hab... muss das const wieder rein?
Du musst den operator auch in einen header stecken
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#ifndef VEKTOR3D #define VEKTOR3D class Vektor3D { public: double x,y,z; Vektor3D(); Vektor3D(double x, double y, double z); ~Vektor3D(); }; Vektor3D operator +(const Vektor3D& left, const Vektor3D& right) { return Vektor3D(left.x + right.x, left.y + right.y, left.z + right.z); } #endif |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include "stdafx.h" #include "Vektor3D.h" Vektor3D::Vektor3D() : x(0.0), y(0.0), z(0.0) { } Vektor3D::Vektor3D(double x, double y, double z) : x(x), y(y), z(z) { } Vektor3D::~Vektor3D(){ } |
C-/C++-Quelltext |
|
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 |
#include "stdafx.h" #include <iostream> #include "Vektor3D.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Vektor3D *v1 = new Vektor3D(); Vektor3D *v2 = new Vektor3D(1.0, 2.0, 3.0); Vektor3D *v3 = new Vektor3D(1.0, 1.0, 1.0); *v1 = *v2 + *v3; cout << "V1: " << v1->x << " " << v1->y << " " << v1->z << endl; cout << "V2: " << v2->x << " " << v2->y << " " << v2->z << endl; cout << "V3: " << v3->x << " " << v3->y << " " << v3->z << endl; Vektor3D v4; Vektor3D v5(1.0, 2.0, 3.0); Vektor3D v6(1.0, 1.0, 1.0); v4 = v5 + v6; cout << "V4: " << v4.x << " " << v4.y << " " << v4.z << endl; cout << "V5: " << v5.x << " " << v5.y << " " << v5.z << endl; cout << "V6: " << v6.x << " " << v6.y << " " << v6.z << endl; system("Pause"); return 0; } |
Werbeanzeige