?
newkid_ schrieb:
du hast schon den operanten überladen, oder? wenn nicht, dann kannst du nicht mit = zuweisen
Bei so einfachen Klassen (ohne dynamische Speicheranforderung) reicht der Default-Kopierkonstruktor. Hier ein Beispiel wie man das Kreuzprodukt innerhalb und ausserhalb der Klasse definieren kann.
#include <iostream>
using namespace std;
template<typename T>
class Vektor3
{
private:
T m_x, m_y, m_z;
public:
Vektor3(T x, T y, T z): m_x(x), m_y(y), m_z(z) {}
Vektor3(T wert): m_x(wert), m_y(wert), m_z(wert) {}
Vektor3(): m_x(0), m_y(0), m_z(0) {}
inline T x() const { return m_x; };
inline T y() const { return m_y; };
inline T z() const { return m_z; };
static Vektor3<T> KreuzProdukt(const Vektor3<T> &v1, const Vektor3<T> &v2)
{
return Vektor3<T>(
v1.m_y*v2.m_z - v1.m_z*v2.m_y,
v1.m_z*v2.m_x - v1.m_x*v2.m_z,
v1.m_x*v2.m_y - v1.m_y*v2.m_x);
}
};
template<typename T>
Vektor3<T> KreuzProduktVonAussen(const Vektor3<T> &v1, const Vektor3<T> &v2)
{
return Vektor3<T>(
v1.y()*v2.z() - v1.z()*v2.y(),
v1.z()*v2.x() - v1.x()*v2.z(),
v1.x()*v2.y() - v1.y()*v2.x());
}
template<typename T>
ostream &operator<<(ostream &ausgabe, const Vektor3<T> &v)
{
return ausgabe << "(" << v.x() << ", " << v.y() << ", " << v.z() << ")";
}
int main()
{
Vektor3<float> a(2,5,7), b(4,6,8), c, d;
c = Vektor3<float>::KreuzProdukt(a,b);
d = KreuzProduktVonAussen(a,c);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cin.get();
}