Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c+中的运算符重载+,无法调用双参数构造函数 我的程序要求我使用C++操作符重载的概念在C++中添加两个有理数。程序应该读取整行(例如:2/3+3/4)并返回结果。程序中的构造函数应该验证有理数(当您在分母中输入0(例如:2/0+3/4)时,应该显示一个错误)_C++ - Fatal编程技术网

c+中的运算符重载+,无法调用双参数构造函数 我的程序要求我使用C++操作符重载的概念在C++中添加两个有理数。程序应该读取整行(例如:2/3+3/4)并返回结果。程序中的构造函数应该验证有理数(当您在分母中输入0(例如:2/0+3/4)时,应该显示一个错误)

c+中的运算符重载+,无法调用双参数构造函数 我的程序要求我使用C++操作符重载的概念在C++中添加两个有理数。程序应该读取整行(例如:2/3+3/4)并返回结果。程序中的构造函数应该验证有理数(当您在分母中输入0(例如:2/0+3/4)时,应该显示一个错误),c++,C++,我写了下面的程序,但我不能调用我的双参数构造函数,所以零参数构造函数正在执行,每次都会打印2的结果。有人能帮我吗 #include<iostream> #include <string> using namespace std; class rational { int numer; int denom; int result; public: rational():numer(1),denom(1) {} ra

我写了下面的程序,但我不能调用我的双参数构造函数,所以零参数构造函数正在执行,每次都会打印2的结果。有人能帮我吗

#include<iostream>
#include <string>
using namespace std;

 class rational { 
     int numer;
     int denom;
     int result;

 public:

    rational():numer(1),denom(1) {}

    rational(int a, int b) {
         if(b==0) {
             cout<<"Eror Denominator should be greater than zero";
             exit(0);
         } else {
             numer=a;
             denom=b;
         }
     }

     int gcd(int a, int b);

     friend istream& operator>>( istream  &input, rational &r ) {        
        int x,y; 
        input>>x;
        input.ignore(1);
        input>>y;

        rational(x,y);  // here I am not able to call my constructor
        return input;     
     }

     rational operator+(rational c2) {
         rational temp;
         rational g;

         temp.numer=(c2.numer*denom)+(numer*c2.denom);
         temp.denom=c2.denom*denom;

         result=g.gcd(temp.numer,temp.denom);
         temp.numer=temp.numer/result;
         temp.denom=temp.denom/result;
         return temp;
     }

     friend ostream &operator<<( ostream &output, const rational &r ) { 
         if(r.denom==1) {
             output <<r.numer;
             return output;            
         } else {
             output <<r.numer<<"/"<<r.denom;
             return output;      
         }
     }
 };

 int rational:: gcd(int a, int b) {
    int gc=1;

     for(int i=1;i<=a&&i<=b;i++) {
          if((a%i==0)&&(b%i==0)) {
              gc=i;
          }
      }
     return gc;
}

 int main() { 
     cout.setf(ios::boolalpha);
     string op;

     rational r1;
     rational r2;


    cin>>r1>>op>>r2;
    cout<<(r1+r2)<<endl;  
    int i;
    cin>>i;
 }
#包括
#包括
使用名称空间std;
类有理{
整数;
int-denom;
int结果;
公众:
rational():numer(1),denom(1){}
有理数(整数a,整数b){
如果(b==0){
cout(istream&input,rational&r){
int x,y;
输入>>x;
输入。忽略(1);
输入>>y;
rational(x,y);//这里我不能调用我的构造函数
返回输入;
}
有理运算符+(有理c2){
合理温度;
有理g;
温度数值=(c2.numer*denom)+(numer*c2.denom);
温度denom=c2.denom*denom;
结果=g.gcd(温度数值、温度数值);
温度数值=温度数值/结果;
温度偏差=温度偏差/结果;
返回温度;
}

friend ostream&operatorFollowing将简单地构造新的
rational
对象并销毁,它不会分配您当前的对象

 rational(x,y);
您需要在重载的
(istream&input,rational&r)
{        
输入>>r.numer>>r.denom;
如果(!r.validate())
抛出std::运行时_错误(“错误输入”);
返回输入;
}
您也可以在参数化构造函数中调用此验证。请记住,当您使用参数调用并使用该对象时,将调用第二个构造函数

请参见本声明中的

rational r1;
调用rational对象r1的默认构造函数

rational():numer(1),denom(1)
{

}
您正在打印:

cout<<(r1+r2)<<endl; 
您需要为对象(类)重载stream操作符>>,然后在其中添加逻辑

 friend istream& operator>>( istream  &input, rational &r )
{ 

     //push the values into object
      input >> r.numer >>r.denom;    
}

你的意思是写
r=rational(x,y);
?当我写上面的代码时,它没有起作用,但是当你试图调用你想要的构造函数时,到底出了什么问题?我需要调用参数化的构造函数,因为这是要求,验证的逻辑应该在构造函数中指定。有什么解决方案吗?你想什么时候调用它??cin>>r1>>op>>r2;o我需要调用参数化构造函数,因为这是要求,验证逻辑应该在构造函数中指定。有解决方案吗?
cout<<(r1+r2)<<endl; 
cin >> r1>> op>> r2;
 friend istream& operator>>( istream  &input, rational &r )
{ 

     //push the values into object
      input >> r.numer >>r.denom;    
}