2016-12-11 3 views
-1

:) Ich bin gerade dabei, eine Übung Übung in Klassen schreiben, und ich stieß auf ein Problem beim Testen meines Codes: Die Eingabe scheint nicht richtig zu funktionieren, da, unabhängig von Welcher Vektor ich eingabe, die Ausgabe gibt immer den (vorzeichenbehafteten, wenn ich mit einem negativen multiplizieren) Nullvektor zurück. Jede Hilfe würde sehr geschätzt werden! Danke :) (Vielleicht bin ich stumpf ist, aber ich sehe nicht, wo das ProblemEigene Vektor Klasse Eingabe Ausgabe

ist
#include <cassert> 
#include <iostream> 
#include <sstream> 
#include <string> 

using namespace std; 

class Vector { 
private: 
    double x, y, z; // Coordinates 
    double scalar; 

public: 
    Vector(double x1, double y1, double z1) { 
     x=x1; y=y1; z=z1; 
    } 

    Vector operator+ (Vector a) { 
     x += a.x; 
     y += a.y; 
     z += a.z; 
     return *this; 
    } 

    friend Vector operator* (Vector a, double scalar) { 
     a.x *= scalar; 
     a.y *= scalar; 
     a.z *= scalar; 
     return a; 
    } 

    friend Vector operator* (double scalar, Vector a) { 
     a.x *= scalar; 
     a.y *= scalar; 
     a.z *= scalar; 
     return a; 
    } 

    friend std::ostream& operator<< (std::ostream& o, Vector a) { 
     o << "(" << a.x << ", " << a.y << ", " << a.z << ")"; 
     return o; 
    } 

    friend std::istream& operator>> (std::istream& i, Vector a) { 
     char c; 
     i >> c >> a.x >> c >> a.y >> c >> a.z >> c; 
     return i; 
    } 

    double get(char i) const { 
     if (i=='x') return x; 
     if (i=='y') return y; 
     if (i=='z') return z; 
    } 

}; 
+1

Das richtige Werkzeug, um solche Probleme zu lösen, ist Ihr Debugger. Sie sollten Schritt für Schritt durch Ihren Code * gehen, bevor Sie auf Stack Overflow nachfragen. Für weitere Hilfe lesen Sie bitte [Wie kleine Programme zu debuggen (von Eric Lippert)] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Zumindest sollten Sie Ihre Frage bearbeiten, um ein [minimales, vollständiges und verifizierbares] (http://stackoverflow.com/help/mcve) Beispiel einzufügen, das Ihr Problem zusammen mit den Beobachtungen, die Sie in der Debugger. –

Antwort

2

denke ich, das Problem in Ihrem operator>> Sie Bezug von Vector wie so passieren sollte.

friend std::istream& operator>> (std::istream& i, Vector& a) 
//             ^

Ansonsten a ist eine lokale Variable und es wird sich nicht ändern.

und btw. Ihre operator+ arbeitet als +=. es sollte neue Vector erstellen.

+1

Vielen Dank - das hat es behoben! Ich konnte einfach nicht sehen, was falsch war mit, wie, zwei Stunden :) Sagt mir, ich muss zurück gehen und Referenzen überprüfen, da das nicht das erste Mal ist, dass ich auf ihnen versaut .. Schönen Tag noch! –

0

Ich habe Ihr Programm nach Ihren Bedürfnissen umstrukturiert. Es gibt jedoch viel Spielraum für Optimierungen.

#include <iostream> 
#include <sstream> 
#include <string> 

class Vector 
{ 
private: 
    double x, y, z; // Coordinates 

public: 
    Vector() : x(0), y(0), z(0) {} 
    Vector(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {} 

    Vector operator+ (Vector const & rhs) const { 
     return Vector(this->x + rhs.x, this->y + rhs.y, this->z + rhs.z); 
    } 

    friend Vector operator* (Vector const & lhs, double s) { 
     return Vector(lhs.x * s, lhs.y * s, lhs.z * s); 
    } 

    friend Vector operator* (double s, Vector const & rhs) { 
     return Vector(rhs.x * s, rhs.y * s, rhs.z * s); 
    } 

    friend std::ostream& operator<< (std::ostream & os, Vector const & a) { 
     os << "(" << a.x << ", " << a.y << ", " << a.z << ")"; 
     return os; 
    } 

    friend std::istream& operator>> (std::istream & is, Vector & a) { // This needs to be a &, to be modified 
     char c; // To dump extra characters 
     is >> c >> a.x >> c >> a.y >> c >> a.z >> c; 
     return is; 
    } 
    double get(char i) const { 
     switch (i) 
     { 
      case 'x' : return x; 
      case 'y' : return y; 
      case 'z' : return z; 
      default : throw std::runtime_error("Vector doesn't contain specified character"); 
     } 
    } 
}; 

int main() 
{ 
    Vector v1 = Vector(2.3, 3.5, 4.7); std::cout << v1 << std::endl; 
    Vector v2 = Vector(1.4, 6.7, 1.1); std::cout << v2 << std::endl; 
    Vector v3 = v1 + v2;    std::cout << v3 << std::endl; 
    Vector v4 = 3 * v1;    std::cout << v4 << std::endl; 
    Vector v5 = v2 * 5;    std::cout << v5 << std::endl; 

    std::cout << v5.get('x') << std::endl; 
    std::stringstream ss; ss << "(1,2,3)"; 
    Vector v6;       std::cout << v6 << std::endl; 
    ss >> v6;       std::cout << v6 << std::endl; 
    Vector v7 = 3 * v6;    std::cout << v7 << std::endl; 
} 

Ausgang -

$ g++ vector.cpp -o vector && ./vector 
(2.3, 3.5, 4.7) 
(1.4, 6.7, 1.1) 
(3.7, 10.2, 5.8) 
(6.9, 10.5, 14.1) 
(7, 33.5, 5.5) 
7 
(0, 0, 0) 
(1, 2, 3) 
(3, 6, 9) 

Nun, durch sie gehen. Wenn Sie weitere Erklärungen benötigen, würde ich gerne helfen.