#include <iostream>
#include <fstream>
using namespace std;
class Jung
{
public:
int c1;
int c2;
int c3;
int c4;
int c5;
int c6;
friend Jung operator+(Jung n1, Jung l1);
friend Jung operator*(Jung n2, Jung l2);
friend ostream &operator<<(ostream &in, Jung n);
friend istream &operator>>(istream &out, Jung &n);
};
Jung operator+(Jung n1, Jung l1){
Jung nuoseklus;
nuoseklus.c5=(1/((1/n1.c1)+(1/n1.c2)+(1/n1.c3)));
return nuoseklus;
}
Jung operator*(Jung n2, Jung l2){
Jung lygiagretus;
lygiagretus.c6=n2.c4;
return lygiagretus;
}
istream &operator>>(istream &in, Jung &n){
in >> n.c1>>n.c2>>n.c3>>n.c4;
return in;
}
ostream &operator<<(ostream &out, Jung n){
out << n.c5<<n.c6;
return out;
}
int main() {
Jung n,i,j;
ifstream myfile("TadasVagonis_EIf_14-2_variantas_03.txt");
cin>>i.c5>>j.c6;
n=i+j;
cout<<n<<endl;
return 0;
}
我有一个文件,我需要从中读取4个数字,在函数operator +和operator *中使用它。第一个函数中的公式是好的,在operator *中我只需要它有c4值。我的最终答案应该是来自运算符+的值加上运算符*的值。因为英语不好,很难解释。
答案 0 :(得分:0)
外部功能的签名是错误的。
应该是
Jung Jung::operator+(Jung RHS){
...
}
Jung Jung::operator*(Jung RHS){
...
}
等等。
之后尝试。希望你能实现你想要的目标
答案 1 :(得分:0)
请编译代码并尝试理解您可以实现的特定行和其余程序的注释。
class Jung
{
public:
int c1, c2;
Jung()
{
// defult constroctor
};
Jung(int tempa, int tempb)
{
c1 = tempa;
c2 = tempb;
}
//now we will do operator overloading
Jung operator+(Jung RHS)
{
c1 = RHS.c1;
c2 = RHS.c2;
return (*this); // read more about this pointer
}
friend ostream& operator<< (ostream &myOut, Jung &Rhs); // to inform the class thta this is a frind function
};
ostream& operator<< (ostream &myOut, Jung &Rhs)
{
myOut<<"value for C1 is :"<<Rhs.c1;
myOut<<"\nValue for C2 is :"<<Rhs.c2;
return myOut;
}
int main()
{
Jung obj1(10,20);
Jung obj2(30,40);
Jung obj3;
obj3 = obj1 + obj2; //Jung operator+(Jung RHS) will get called
cout<<obj3; // ostream &operator<<(ostream &myOut, Jung &Rhs) will get called
return 0;
}