4 Replies - 597 Views - Last Post: 07 April 2013 - 03:47 PM Rate Topic: -----

#1 Jite25  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 24-February 13

Rational Class Dev c++ errors

Posted 06 April 2013 - 07:36 PM

I'm working on the last weekly assingment for my c++ class and I'm getting some errors in Dev C++ when compiling. The instructor made a video showing us how to do half the assignment, so I followed the example exactly. However, I get
26  `Rational::Rational()' and `Rational::Rational()' cannot be overloaded

97  `void Rational::setDenom(int)' and `void Rational::setDenom(int)' cannot be overloaded 

 In member function `Rational Rational::add(Rational)': 

47 no match for 'operator/' in '((Rational*)this)->Rational::num / ((Rational*)this)->Rational::gcd(((Rational*)this)->Rational::num, ((Rational*)this)->Rational::denom)' 




to name a few. There is no program yet, just creating the class and class member functions


#include <iostream> // input and output
#include <stdlib.h> // pause screen after completion
#include <math.h>
#include <iomanip>

using namespace std;

class Rational{
private:
	int num;
	int denom;
public:
	Rational();
	Rational(int n, int d);
	Rational add(Rational c);
	Rational multiply(Rational c);
    void setDenom(int d);
    
Rational()
{

num = 1;
denom = 1;

}

Rational(int n, int d)

{
	num = n;
	if(d != 0)
        denom = d; 
    else
        cout << "erorr: Denominator cannot be 0";
}

Rational add(Rational x)
{
     Rational temp(num, denom); // create an object with 1/3
     temp.num = temp.num + x.num; // add 2/3 object to the 1/3 = 3/3 
     return Rational (num/gcd(num,denom),denom/gcd(num,denom));
     //return temp;      // return 3/3    
   }

Rational subtract(Rational x)
{
     Rational temp(num, denom); // create an object with 1/3
     temp.num = temp.num - x.num; // subtract 2/3 object to the 1/3 = 1/3 
     return Rational (num/gcd(num,denom),denom/gcd(num,denom));
    // return temp;      // return 1/3    
   }
   
Rational multiply(Rational x)

{
      Rational temp(num, denom);
      temp.num = temp.num  * x.num;
      temp.denom = temp.denom * x.denom;
      return Rational (num/gcd(num,denom),denom/gcd(num,denom));
      //return temp;
      
         }
Rational divide(Rational x)

{
      Rational temp(num, denom);
      
      temp.num  = temp.denom; // make reciprocal
      temp.denom = temp.num;
      
      temp.num = temp.num  * x.num; // multiply by the reciprocal
      temp.denom = temp.denom * x.denom;
      
      return Rational (num/gcd(num,denom),denom/gcd(num,denom));
      //return temp;
      
      
         }
    
   /* Rational reduce(Rational x)

{
      Rational temp(num, denom);
      
      r = [(temp.num) && (temp.denom) / gcd(temp.num,temp.denom)];
      return temp;
      
         } */
         
  void setDenom(int d)
  {
      if(d != 0)
           denom = d; 
       }   
       
       
Rational gcd (int num, int denom){

while( denom != 0) {

//gcd = temp.num % temp.denom;
gcd = num % denom;
num = denom;
denom = gcd;

return gcd;
}


         
}



Is This A Good Question/Topic? 0
  • +

Replies To: Rational Class Dev c++ errors

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Rational Class Dev c++ errors

Posted 06 April 2013 - 11:24 PM

All your statements in public:

need to go as your attempting to overload Rational

and your declaring them inside the class.

you have a lot of other errors however.

read this on overloading constructors

http://www.cplusplus...torial/classes/

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 Jite25  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 24-February 13

Re: Rational Class Dev c++ errors

Posted 06 April 2013 - 11:31 PM

View Postsnoopy11, on 06 April 2013 - 11:24 PM, said:

All your statements in public:

need to go as your attempting to overload Rational

and your declaring them inside the class.

you have a lot of other errors however.

read this on overloading constructors

http://www.cplusplus...torial/classes/

Snoopy.



The strange thing is the teacher did the same thing and he didnt overload it! Or he didn't say! If I remove everything in public, does that change what things are public? Or is it like if something is not private, it is AUTOMATICALLY PUBLIC?
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Rational Class Dev c++ errors

Posted 06 April 2013 - 11:33 PM

No I meant

public:


Rational()
{

num = 1;
denom = 1;

}


read the link would you

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 Jite25  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 82
  • Joined: 24-February 13

Re: Rational Class Dev c++ errors

Posted 07 April 2013 - 03:47 PM

Ok I've fixed that overload issue. It turns out the instructor video was showing how to put things in public if you didn't use them elsewhere. Any other errors? I think my return statements in the functions are wrong. They are supposed to give the reduced answer of the fraction operation (like add, multiply,etc.).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1