プログラムの挿入演算子のオーバーロードであると思われるものに問題があります。これは、複素数とベクトルを使用してタスクを実行するために子孫関数を使用することになっている初心者の C++ クラスの課題です。いずれかのクラスに数値を入力すると、正しく読み取られていないか、配列に正しく割り当てられていません。私はこれを1時間以上整理しようとしてきましたが、何もうまくいかないようです。
クラス:
class pairs
{
protected:
double a;
double b;
public:
pairs(): a(0), b(0){}
pairs(const pairs&p): a(p.a), b(p.b){}
pairs(double x, double y): a(x), b(y){}
pairs operator +(pairs& second){
return pairs(a+second.a, b+second.b);
}
pairs operator -(pairs& second){
return pairs(a-second.a, b-second.b);
}
bool operator ==(pairs& second){
bool tf=false;
if (a==second.a && b==second.b)
tf=true;
return tf;
}
};
class comp:public pairs
{
public:
comp():pairs(){}
comp(const pairs&p):pairs(p){}
comp(double x, double y):pairs(x, y){}
comp operator *(comp& second){
comp mew;
mew.a=(a*second.a)-(b*second.b);
mew.b=(a*second.b)+(b*second.a);
return mew;
}
comp operator /(comp& second);
friend ostream& operator << (ostream& fout, comp& num){
if(num.b<0)
cout<<num.a<<num.b<<"i";
else
cout<<num.a<<"+"<<num.b<<"i";
return fout;
}
friend istream& operator >> (istream& fin, comp num){
char sym, i;
cout<<"Enter a complex number in a+bi or a-bi form: ";
cin>>num.a>>sym>>num.b>>i;
if(sym=='-')
num.b*=-1;
return fin;
}
};
class vect:public pairs
{
public:
vect():pairs(){}
vect(const pairs&p):pairs(p){}
vect(double x, double y):pairs(x, y){}
vect operator*(double num){
return vect(a*num, b*num);
}
int operator*(vect num){
int j;
j=(a*num.a)+(b*num.b);
return j;
}
friend ostream& operator << (ostream& fout, vect& num){
cout<<"<"<<num.a<<","<<num.b<<">";
return fout;
}
friend istream& operator >> (istream& fin, vect num){
char beak, com;
cout<<"Enter vector in <a,b> form: ";
cin>>beak>>num.a>>com>>num.b>>beak;
return fin;
}
};
挿入演算子の呼び出しは次のようになります。
comp temp;
int store;
cin>>temp;
cout<<"Where do you want to store this (enter 1-6): ";
cin>>store;
while(store<1 || store>6)
{
cout<<"Invalid location. re-enter: ";
cin>>store;
}
six[store-1]=temp;
break;
'comp temp' を 'vect temp' に変更すると、vect でも同じです。サイズ 6 の comp または vect の配列が関数に渡されます。
プログラムを実行して、配列に割り当てられる前にtempを印刷しようとしましたが、両方の値がまだゼロであり、その理由がわかりません。
どんなアドバイスでも大歓迎です。:]