C ++中的类型转换(Rational数字类)错误使用' +'操作者

时间:2014-11-07 03:40:04

标签: c++ operator-overloading typecasting-operator

我正在努力重载' +'一个有理类的运算符,这样我也可以在我的有理类和三个数值(int,double和float)之间进行类型转换。当我将数字加在一起时,我遇到了一个问题。当有理数(a / b,其中a和b是整数,b!= 0)被强制转换为float,double或int值时,类型转换就可以正常工作。但是,当尝试创建三个数值(double,int或float)的变量并将该变量设置为'其各自的类型' +'有理数'我收到这些错误:

|In member function ‘rational rational::operator+(rational)’:|
|56|error: request for member ‘denom’ in ‘return_val’, which is of non-class type ‘rational()’|
|57|error: request for member ‘num’ in ‘return_val’, which is of non-class type ‘rational()’|
|58|error: request for member ‘reduce’ in ‘return_val’, which is of non-class type ‘rational()’|
|59|error: invalid conversion from ‘rational (*)()’ to ‘int’ [-fpermissive]|
|19|error:   initializing argument 1 of ‘rational::rational(int)’ [-fpermissive]|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

这是我的代码:(main.cpp,rational.cpp,rational.h):

Main.cpp的

#include <iostream>
#include "rational.h"

using namespace std;

int main()
{
    rational rhs(1,4);

    double x = rhs;

    double y = x + 3.5;

    cout << endl << y;

    return 0;
}

rational.h

/*
 Description: The class declaration for 'rational class' -- which will overload operators and a allow for
            a natural syntax of operations for the rational class.
 */


#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
#include <ostream>

using namespace std;

class rational {
 private:
  int num, denom; //declares the numerator and denominator

 public:
  /**** Constructors ****/
  rational(); //the main constructor, sets the numerator and denominator equal to arbitrary integers where the denominator != 0.
  rational(int numerator, int denominator); //sets the private numerator and denominator equal to num and denom, respectively.


  /**** Utilities ****/
  void reduce(); //simplifies the fraction by dividing the numerator and denominator by their greatest common factor
  void print(); //test function
  int gcd();

  /**** Overloaded Operators ****/
  void operator=(rational rhs); //overloads the '=' operator for assignment of rational class
  rational operator+(rational rhs); //overloads the '+' operator to work with objects of rational class
  rational operator-(rational rhs); //overloads the '-' operator to work with objects of rational class
  rational operator*(rational rhs); //overloads the '*' operator to work with objects of rational class
  rational operator/(rational rhs); //overloads the '/' operator to work with objects of rational class


  // Friend functions  - allows ostream to access rational under the operator <<
  friend ostream& operator<<(ostream &stream, rational rhs); //prints the rational to screen in form "num/denom"

  /**** Overloaded Typecast ****/
  operator int();   //allows the rational class to be typecast from rational to int
  operator float(); //allows the rational class to be typecast from rational to float
  operator double();//allows the rational class to be typecast from rational to double
  rational(int numerator); // turns an integer into a rational value by placing it over a denominator of 1

};

#endif

rational.cpp

/*
Description: rational.cpp -- the implementation file for the rational class' methods
*/

#include "rational.h"
#include <iostream>
#include <ostream>  // made friend

using namespace std;

rational::rational(){
    num = 1;
    denom = 1;
}

rational::rational(int numerator){
    num = numerator;
    denom = 1;
}

rational::rational(int numerator, int denominator){
    num = numerator;
    denom = denominator;
}
int rational::gcd(){
    int gcd, temp_num, temp_denom;
    temp_num = num;
    temp_denom = denom;
    while (temp_denom != 0){
        gcd = temp_denom;
        temp_denom = temp_num%temp_denom;
        temp_num = gcd;
    }
    return temp_num;
}

void rational::reduce(){
                        //how to implement into arithmetic? unspecific rational class...
   int factor = gcd();
    ///Reducing With Found GCD
    num = num/factor;
    denom = denom/factor;
}

void rational::operator=(rational rhs){
    num = rhs.num;
    denom = rhs.denom;
}

rational rational::operator+(rational rhs){
    rational return_val();

    return_val.denom = denom * rhs.denom; //multiply both denominators by each other
    return_val.num = (num * rhs.denom) + (rhs.num * denom); // multiply the numerators by their opposing denominators
    return_val.reduce();
    return return_val;
}

rational rational::operator-(rational rhs){
    rational return_val;

    return_val.denom = denom * rhs.denom; //multiply both denominators by each other
    return_val.num = (num * rhs.denom) - (rhs.num * denom); //multiply the numerators by their opposing denominators
    return_val.reduce();
    return return_val;
}

rational rational::operator*(rational rhs){
    rational return_val;

    return_val.denom = denom * rhs.denom; //normal rational multiplication
    return_val.num = num * rhs.num;
    return_val.reduce();
    return return_val;

}

rational rational::operator/(rational rhs){
    rational return_val;

    return_val.denom = denom * rhs.num; //normal rational division (multiply by reciprical)
    return_val.num = num * rhs.denom;
    return_val.reduce();
    return return_val;
}

ostream& operator<<(ostream &stream, rational rhs){
    return stream << rhs.num << "/" << rhs.denom;
}

rational::operator int(){
    return (num/denom);  //divides the numerator by denominator (flooring to the nearest integer)
}

rational::operator double(){
    return (num/(denom*1.0)); //divides the numerator by denominator(in double form)
}

rational::operator float(){
    return (num/(denom*1.f)); //divides the numerator by denominator(in float form)
}

0 个答案:

没有答案