2016-12-11 9 views
-1

:)私は現在、クラスを書く上で練習問題を練習中です。コードをテストするときに問題が発生しました。入力が正常に動作していないように見えます。どのベクトルを入力すると、出力は常にゼロベクトルを(負の数で乗算した場合は符号付き)を返します。どんな助けも高く評価されるでしょう!おそらく、私が鈍角であること午前(:)ありがとうございますが、問題は独自のベクトルクラスの入力問題

#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

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低でも、あなたはあなたが行った観察と一緒に、[編集]あなたの質問あなたの問題を再現[、最小完全、かつ検証](http://stackoverflow.com/help/mcve)の例を含むようにする必要があります\しますデバッガ。 –

答えて

2

私はあなたのoperator>>で問題を考えるあなたがそうのようなVectorの参照を渡す必要がある場合、私は見ていないです:。

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

は、そうでない場合aはローカル変数であり、それは変更されません。

そして、ところで。+=としてのあなたのoperator+作品。これは、新しいVector

0を作成する必要があります
+1

ありがとうございました - それを修正しました!私はちょうど2時間の間それが間違っているのを見ることができませんでした:)私は私がそれらを台無しにすることは初めてではないので、私が戻って参照をレビューする必要があることを教えてください..いい日を! –

0

あなたのニーズに合わせてプログラムをリファクタリングしました。しかし、最適化のための範囲がたくさんあります。

#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; 
} 

出力 -

$ 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) 

まあ、それを介して行くのです。それ以上の説明が必要な場合は、お手伝いします。

関連する問題