2

序章:

タイトルがややこしかったり曖昧だったりしたらすみません。私が持っている質問が検索可能な用語に分解されていないように見えるため、私の問題についてインターネット検索を行うのは非常に困難でした. また、これは Stackoverflow に関する私の最初の投稿です。質問を投稿するという従来の考え方から外れている場合は、ご容赦ください。賢明にフォーマットできるように最善を尽くします。

そうは言っても、私がやろうとしていることに入りましょう:

私は、講師から与えられた課題に取り組んでいる大学の学生です。ベクトル用のクラスを作成しています (つまり、データ型のベクトルではなく、数学的に言えばベクトルです)。このクラスは、ベクトルの x コンポーネント用とベクトルの y コンポーネント用の 2 つの異なるテンプレート データ型のクラス テンプレートになります。これは単純なクラスで、ベクトルの大きさと方向 (ラジアン単位) を返します。フレンド関数としてオーバーロードされた入力演算子と出力演算子、およびいくつかのコンストラクターもあります。私は動的メモリを扱っていないので、ワームの缶全体を脇に置くことができます。

これが私が抱えている問題です:

Vector2D<int, int> vec1(); //Default Constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";

私の問題は、cin を実行できず、方向 () と大きさ () を出力できないことです。コンパイラは非常に長いエラーを出しますが、基本的には

エラー C2678: バイナリ '>>' : 型 'std::istream' の左側のオペランドを取る演算子が見つかりません (または、受け入れ可能な変換がありません)

ただし、これを行う場合:

Vector2D<int, int> vec1(0,0); //No longer the default constructor
cin  >> vec1;
cout << "\nVector 1 = " << vec1 << "\n\tDirection: " << vec1.direction()
     << "\tMagnitude: " << vec1.magnitude() << "\n\n";

すべてが世界に満足しています。したがって、私の質問は、どうすればこれを修正できますか? デフォルトのコンストラクターでインスタンス化した後に cin を使用したいのですが、方向 () と大きさ () を出力したいと思います。すべてのヘッダー宣言とその他すべてを正しく行い、クラスの書き方が間違っていると考えてください。これは次のとおりです。

私のクラスファイル:

#pragma once
#include <iostream>
#include <iomanip>
#include <cmath>    //for sqrt function to get the magnitude and atan for radians.

using namespace std;

template <class T, class S>
class Vector2D
{
private:
    T m_xComp;
    S m_yComp;
    static int signif_digit; //Becomes the argument for setPrecision(x) on output.
public:
    static int signif_digits;
    Vector2D(): m_xComp((T)0), (S)m_yComp((S)0) {};
    Vector2D(T xComp, S yComp);
    void setX(T xComp);
    void setY(S yComp);
    T getX();
    S getY();
    double magnitude();
    double direction(); //returns direction of vector in radians.
    static void setPrecision(int prec);
    static int precision();
    friend ostream& operator<<(ostream& os, const Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        os  << '<' << vec.m_xComp << ',' << vec.m_yComp << '>';
        return os;
    }

    friend istream& operator>>(istream& is, Vector2D<T,S>& vec)
    { //A good thing to figure out: Why did I have to declare friend functions in line?
        char remove_Char;
        T xComp = 0;
        S yComp = 0;
        is >> remove_Char >> xComp >> remove_Char >> yComp;
        vec.m_xComp = xComp;
        vec.m_yComp = yComp;
        return is;
    }
};


template <class T, class S>
Vector2D<T, S>::Vector2D(T xComp, S yComp)
{
    m_xComp = xComp;
    m_yComp = yComp;
}


template <class T, class S>
void Vector2D<T, S>::setPrecision(int prec)
{
    signif_digit = prec;
}


template <class T, class S>
int Vector2D<T, S>::precision()
{   return signif_digit;    }


template <class T, class S>
void Vector2D<T, S>::setX(T xComp)
{   m_xComp = xComp;    }


template <class T, class S>
void Vector2D<T, S>::setY(S yComp)
{   m_yComp = yComp;    }


template <class T, class S>
T Vector2D<T, S>::getX()
{   return m_xComp;     }


template <class T, class S>
S Vector2D<T, S>::getY()
{   return m_yComp;     }


template <class T, class S>
double Vector2D<T, S>::magnitude()
{
    return sqrt( (double)(m_xComp*m_xComp + m_yComp*m_yComp) );
}


//------------------------Consider using atan2 next time-------------------------------------
template <class T, class S>
double Vector2D<T, S>::direction()
{
    if (m_xComp == 0)
    {
        if(m_yComp == 0)
        {
            cout << "\nNote: Both x and y components equal zero.\n";
            return 0;
        }
        else if (m_yComp > 0)
            return atan(1.0)*2; //If y > 0 and x = 0, return PI/2
        else if (m_yComp < 0)
            return atan(1.0)*6; //If y < 0 and x = 0, return 3*PI/2
    }
    else if (m_xComp > 0)
    {
        if (m_yComp >= 0)
            return atan((double)(m_yComp/m_xComp)); //First Quadrant
        else
            return (atan(1.0)*8 + atan((double)(m_yComp/m_xComp)) ); //Fourth Quadrant
    }
    else
        return (atan(1.0)*4 + atan((double)(m_yComp/m_xComp)) ); //Second & Third Quadrant
}
//-------------------------------------------------------------------------------------------


template <class T, class S>
int Vector2D<T, S>::signif_digit = 3;   //private

template <class T, class S>
int Vector2D<T, S>::signif_digits = 3;  //public

それだけです。含める必要がある追加情報がある場合はお知らせください。

ありがとう。

4

1 に答える 1