10 Replies - 756 Views - Last Post: 28 February 2010 - 10:33 AM Rate Topic: -----

#1 Guest_ohs14*


Reputation:

I got error messages but dont know how to fix them

Posted 27 February 2010 - 10:57 PM

I put ///////////////////////// right next to error
The program is designed to get add,subtract,multiply (fractions)...

and does anyone know how to get division?

#include "stdafx.h"
#ifndef FRACTION_H__
#define FRACTION_H__

#include <iostream>

#include <assert.h>

 

using namespace std;

 

class Fraction {
 public:
    Fraction();    //default constructor    

    Fraction(int num, int denom);     // explicit value constructor
   Fraction(Fraction& f);      // copy constructor
  ~Fraction();        // destructor


    // overloaded operators
    friend bool operator == (const Fraction & f1, const Fraction & f2);
    Fraction operator+(const Fraction &f1) const;
    Fraction operator-(const Fraction &f1) const;
    Fraction operator*(const Fraction &f1) const;
    const Fraction& operator=(const Fraction &f);
 bool operator == ( Fraction & f2);

 
    
 
    // friend functions 
    friend std::istream& operator>> (std::istream& in, Fraction & f);
    friend std::ostream& operator<< (std::ostream& out, const Fraction& f);

 private:
    int num;  // better design is make these private
    int denom; // and provide accessor and mutator methods

 public:
 int GetNum() { return num; }
 int GetDenom() { return denom; }
 void SetNum( int nNum ) { num = nNum; }
 void SetDenom( int nDenom ) { denom = nDenom; }

};

#endif // FRACTION_H__


Fraction::Fraction(): num(1), denom(1) {}

 

// explicit value constructor
Fraction::Fraction(int numIn, int denomIn) {
    num = numIn;
    denom = denomIn;
}

// copy constructor
Fraction::Fraction(Fraction& f) {          [b]/////////note: candidates are: Fraction::Fraction(Fraction&)[/b]    num = f.GetNum();
    denom = f.GetDenom();
}


Fraction Fraction::operator+(const Fraction &f) const {
    Fraction result;
    result.num = num * f.denom + denom * f.num;
    result.denom = denom * f.denom;
    return result;

}

Fraction Fraction::operator-(const Fraction &f) const {
    Fraction result;
    result.num = num * f.denom - denom * f.num;
    result.denom = denom * f.denom;
    return result;

}

Fraction Fraction::operator*(const Fraction &f) const {
    Fraction result;
    result.num = num * f.num;
    result.denom = denom * f.denom;
    return result;

}


bool Fraction::operator == ( Fraction & f2) {
    //   return  (f1.num == f2.num &&
    //     f1.denom == f2.denom);
    return (this->GetNum() * f2.GetDenom() == f2.GetNum() * this->GetDenom() );
} 


// in.good() specifies that the input was successfull
// the right type was received

istream& operator >> (istream & in, Fraction & f) {
  int numIn, denomIn;
  cout << "Enter the numerator: ";
  in >> numIn;
  assert(in.good());
  f.SetNum( numIn );
  
  cout << "Enter the denominator: ";
  in >> denomIn;
  assert(in.good());
  cout << "denomIn is: " << denomIn << endl;
  f.SetDenom( denomIn );

  return in;
}


ostream& operator << (ostream& out, Fraction& f) {
    out << "Fraction is: " << f.GetNum() << '/'  << f.GetDenom() << endl;
    return out;
}


// If f1, f2, f3 are of type Fraction, then 
// it is possible to write code in this form: f1 = f2 = f3;
// because the return from this method is of type Fraction.
// Also, note the use of "*this". This is the way 
// to return an instance of the current object as a return value


const Fraction& Fraction::operator=(const Fraction& f) {
  if (this != &f) {
    num = f. num;
    denom = f.denom;
  }
  return *this;
}

Fraction::~Fraction() {
}

#define CAPACITY 10

int main ( ) {

 int nNum;
 int nDenom;
    //int CAPACITY = 10;
    char string[CAPACITY];
    
    Fraction f1;
    Fraction f2;

        cout<<"Test Your Math \n" << endl;
        cout<<"Enter one operation of add, subtract, multiply, equal, quit \n" <<endl;
        cout<<"COMMAND>" <<endl;
        cin>>string;
         
        cin>>nNum;
  cin>>nDenom;

  f1.SetNum( nNum );
  f1.SetDenom( nDenom );

        cout<<f1.GetNum() << endl << f1.GetDenom() << endl;

  cin>>nNum;
  cin>>nDenom;

  f2.SetNum( nNum );
  f2.SetDenom( nDenom );

        cout<<f2.GetNum() << endl << f2.GetDenom() << endl;
        
        switch(strlen(string))
  {
              case 3:
    {
                     Fraction f3=f1+f2;      [b]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The sum of Fraction1 & Fraction2 is"<< f3.GetNum() << endl << f3.GetDenom() <<endl;
      break;
    }
                     
         
              case 4:      
                     break;

              case 5:  
      {
                     int f3 = (f1==f2);
                     if(f3 == 1)
      cout<<"The Fraction1 & Fraction2 is same"<<endl;
                     else if(f3 == 0)
      cout<<"The Fraction1 & Fraction2 is same"<<endl;
                     break; 
      }
       
              case 8:
                     if( string[0] = 109){
                     Fraction f3=f1*f2;    [b]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The multiply of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;       
                     }
                     else{ 
                     Fraction f3=f1-f2;   [b]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The subtract of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;       
                     }
                     break;
  }
  return 0;
}





Is This A Good Question/Topic? 0

Replies To: I got error messages but dont know how to fix them

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: I got error messages but dont know how to fix them

Posted 27 February 2010 - 11:04 PM

Division is /.

Incidentally, a C fraction can be represented literally as numerator/denominator. For example, 3/4 is equal to 3 quarters.
Was This Post Helpful? 0
  • +
  • -

#3 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: I got error messages but dont know how to fix them

Posted 27 February 2010 - 11:05 PM

What was the error you got?
Was This Post Helpful? 0
  • +
  • -

#4 ohs14  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 13-October 09

Re: I got error messages but dont know how to fix them

Posted 27 February 2010 - 11:29 PM

I got these error messages. I underlined the error
note: candidates are: Fraction::Fraction(Fraction&)
error: no matching function for call to ?raction::Fraction(Fraction)
if you scroll right little bit you will see such as "[u]///////" which is error message

This program is designed to add, subtract, multiply (fraction).

#include "stdafx.h" 
#ifndef FRACTION_H__ 
#define FRACTION_H__ 
 
#include <iostream> 
 
#include <assert.h> 
 
  
 
using namespace std; 
 
  
 
class Fraction { 
 public: 
    Fraction();    //default constructor     
 
    Fraction(int num, int denom);     // explicit value constructor 
   Fraction(Fraction& f);      // copy constructor 
  ~Fraction();        // destructor 
 
 
    // overloaded operators 
    friend bool operator == (const Fraction & f1, const Fraction & f2); 
    Fraction operator+(const Fraction &f1) const; 
    Fraction operator-(const Fraction &f1) const; 
    Fraction operator*(const Fraction &f1) const; 
    const Fraction& operator=(const Fraction &f); 
 bool operator == ( Fraction & f2); 
 
  
     
  
    // friend functions  
    friend std::istream& operator>> (std::istream& in, Fraction & f); 
    friend std::ostream& operator<< (std::ostream& out, const Fraction& f); 
 
 private: 
    int num;  // better design is make these private 
    int denom; // and provide accessor and mutator methods 
 
 public: 
 int GetNum() { return num; } 
 int GetDenom() { return denom; } 
 void SetNum( int nNum ) { num = nNum; } 
 void SetDenom( int nDenom ) { denom = nDenom; } 
 
}; 
 
#endif // FRACTION_H__ 
 
 
Fraction::Fraction(): num(1), denom(1) {} 
 
  
 
// explicit value constructor 
Fraction::Fraction(int numIn, int denomIn) { 
    num = numIn; 
    denom = denomIn; 
} 
 
// copy constructor 
Fraction::Fraction(Fraction& f) {          [u]/////////note: candidates are: Fraction::Fraction(Fraction&)[/u]    
    num = f.GetNum(); 
    denom = f.GetDenom(); 
} 
 
 
Fraction Fraction::operator+(const Fraction &f) const { 
    Fraction result; 
    result.num = num * f.denom + denom * f.num; 
    result.denom = denom * f.denom; 
    return result; 
 
} 
 
Fraction Fraction::operator-(const Fraction &f) const { 
    Fraction result; 
    result.num = num * f.denom - denom * f.num; 
    result.denom = denom * f.denom; 
    return result; 
 
} 
 
Fraction Fraction::operator*(const Fraction &f) const { 
    Fraction result; 
    result.num = num * f.num; 
    result.denom = denom * f.denom; 
    return result; 
 
} 
 
 
bool Fraction::operator == ( Fraction & f2) { 
    //   return  (f1.num == f2.num && 
    //     f1.denom == f2.denom); 
    return (this->GetNum() * f2.GetDenom() == f2.GetNum() * this->GetDenom() ); 
}  
 
 
// in.good() specifies that the input was successfull 
// the right type was received 
 
istream& operator >> (istream & in, Fraction & f) { 
  int numIn, denomIn; 
  cout << "Enter the numerator: "; 
  in >> numIn; 
  assert(in.good()); 
  f.SetNum( numIn ); 
   
  cout << "Enter the denominator: "; 
  in >> denomIn; 
  assert(in.good()); 
  cout << "denomIn is: " << denomIn << endl; 
  f.SetDenom( denomIn ); 
 
  return in; 
} 
 
 
ostream& operator << (ostream& out, Fraction& f) { 
    out << "Fraction is: " << f.GetNum() << '/'  << f.GetDenom() << endl; 
    return out; 
} 
 
 
// If f1, f2, f3 are of type Fraction, then  
// it is possible to write code in this form: f1 = f2 = f3; 
// because the return from this method is of type Fraction. 
// Also, note the use of "*this". This is the way  
// to return an instance of the current object as a return value 
 
 
const Fraction& Fraction::operator=(const Fraction& f) { 
  if (this != &f) { 
    num = f. num; 
    denom = f.denom; 
  } 
  return *this; 
} 
 
Fraction::~Fraction() { 
} 
 
#define CAPACITY 10 
 
int main ( ) { 
 
 int nNum; 
 int nDenom; 
    //int CAPACITY = 10; 
    char string[CAPACITY]; 
     
    Fraction f1; 
    Fraction f2; 
 
        cout<<"Test Your Math \n" << endl; 
        cout<<"Enter one operation of add, subtract, multiply, equal, quit \n" <<endl; 
        cout<<"COMMAND>" <<endl; 
        cin>>string; 
          
        cin>>nNum; 
  cin>>nDenom; 
 
  f1.SetNum( nNum ); 
  f1.SetDenom( nDenom ); 
 
        cout<<f1.GetNum() << endl << f1.GetDenom() << endl; 
 
  cin>>nNum; 
  cin>>nDenom; 
 
  f2.SetNum( nNum ); 
  f2.SetDenom( nDenom ); 
 
        cout<<f2.GetNum() << endl << f2.GetDenom() << endl; 
         
        switch(strlen(string)) 
  { 
              case 3: 
    { 
                     Fraction f3=f1+f2;      [u]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/u]
                     cout<<"The sum of Fraction1 & Fraction2 is"<< f3.GetNum() << endl << f3.GetDenom() <<endl; 
      break; 
    } 
                      
          
              case 4:       
                     break; 
 
              case 5:   
      { 
                     int f3 = (f1==f2); 
                     if(f3 == 1) 
      cout<<"The Fraction1 & Fraction2 is same"<<endl; 
                     else if(f3 == 0) 
      cout<<"The Fraction1 & Fraction2 is same"<<endl; 
                     break;  
      } 
        
              case 8: 
                     if( string[0] = 109){ 
                     Fraction f3=f1*f2;    [b]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/u] 
                     cout<<"The multiply of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;        
                     } 
                     else{  
                     Fraction f3=f1-f2;   [u]/////// error: no matching function for call to ?raction::Fraction(Fraction)[/u] 
                     cout<<"The subtract of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;        
                     } 
                     break; 
  } 
  return 0; 
}


This post has been edited by ohs14: 27 February 2010 - 11:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: I got error messages but dont know how to fix them

Posted 27 February 2010 - 11:33 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:


  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#6 ohs14  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 13-October 09

Re: I got error messages but dont know how to fix them

Posted 27 February 2010 - 11:42 PM

If you scroll right a little bit, you will see error message
I put like "////////////// note: ~~~`" and "/////////error:~~~~~"
Was This Post Helpful? 0
  • +
  • -

#7 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: I got error messages but dont know how to fix them

Posted 28 February 2010 - 12:25 AM

Did you include the header containing Fraction class, to your main() and to the class implementation .cpp?
EDIT: I just did that, and your program seems to compile with no warning (I'm using MSVC 2008 btw)

This post has been edited by sarmanu: 28 February 2010 - 12:33 AM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,069
  • Joined: 10-May 07

Re: I got error messages but dont know how to fix them

Posted 28 February 2010 - 12:31 AM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: I got error messages but dont know how to fix them

Posted 28 February 2010 - 07:13 AM

Merged apparently duplicate topics.
Also, please go back to your book and check the proper way to define a copy constructor, as yours is incorrect, which is the source of your issues here.
Was This Post Helpful? 0
  • +
  • -

#10 ohs14  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 13-October 09

Re: I got error messages but dont know how to fix them

Posted 28 February 2010 - 10:28 AM

Did you include the header containing Fraction class, to your main() and to the class implementation .cpp?
EDIT: I just did that, and your program seems to compile with no warning (I'm using MSVC 2008 btw)


I just checked if I included the header but for me, it seems fine and i dont know what's wrong......
sarmanu, can you please show how you did?

This post has been edited by ohs14: 28 February 2010 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: I got error messages but dont know how to fix them

Posted 28 February 2010 - 10:33 AM

fraction.h
#ifndef FRACTION_H__
#define FRACTION_H__

#include <iostream>

#include <assert.h>

 

using namespace std;

 

class Fraction {
 public:
    Fraction();    //default constructor    

    Fraction(int num, int denom);     // explicit value constructor
   Fraction(Fraction& f);      // copy constructor
  ~Fraction();        // destructor


    // overloaded operators
    friend bool operator == (const Fraction & f1, const Fraction & f2);
    Fraction operator+(const Fraction &f1) const;
    Fraction operator-(const Fraction &f1) const;
    Fraction operator*(const Fraction &f1) const;
    const Fraction& operator=(const Fraction &f);
 bool operator == ( Fraction & f2);

 
    
 
    // friend functions 
    friend std::istream& operator>> (std::istream& in, Fraction & f);
    friend std::ostream& operator<< (std::ostream& out, const Fraction& f);

 private:
    int num;  // better design is make these private
    int denom; // and provide accessor and mutator methods

 public:
 int GetNum() { return num; }
 int GetDenom() { return denom; }
 void SetNum( int nNum ) { num = nNum; }
 void SetDenom( int nDenom ) { denom = nDenom; }

};

#endif // FRACTION_H__



fraction.cpp
#include "fraction.h"

Fraction::Fraction(): num(1), denom(1) {}

 

// explicit value constructor
Fraction::Fraction(int numIn, int denomIn) {
    num = numIn;
    denom = denomIn;
}

// copy constructor
Fraction::Fraction(Fraction& f) {          /////////note: candidates are: Fraction::Fraction(Fraction&)[/b]    num = f.GetNum();
    denom = f.GetDenom();
}


Fraction Fraction::operator+(const Fraction &f) const {
    Fraction result;
    result.num = num * f.denom + denom * f.num;
    result.denom = denom * f.denom;
    return result;

}

Fraction Fraction::operator-(const Fraction &f) const {
    Fraction result;
    result.num = num * f.denom - denom * f.num;
    result.denom = denom * f.denom;
    return result;

}

Fraction Fraction::operator*(const Fraction &f) const {
    Fraction result;
    result.num = num * f.num;
    result.denom = denom * f.denom;
    return result;

}


bool Fraction::operator == ( Fraction & f2) {
    //   return  (f1.num == f2.num &&
    //     f1.denom == f2.denom);
    return (this->GetNum() * f2.GetDenom() == f2.GetNum() * this->GetDenom() );
} 


// in.good() specifies that the input was successfull
// the right type was received

istream& operator >> (istream & in, Fraction & f) {
  int numIn, denomIn;
  cout << "Enter the numerator: ";
  in >> numIn;
  assert(in.good());
  f.SetNum( numIn );
  
  cout << "Enter the denominator: ";
  in >> denomIn;
  assert(in.good());
  cout << "denomIn is: " << denomIn << endl;
  f.SetDenom( denomIn );

  return in;
}


ostream& operator << (ostream& out, Fraction& f) {
    out << "Fraction is: " << f.GetNum() << '/'  << f.GetDenom() << endl;
    return out;
}


// If f1, f2, f3 are of type Fraction, then 
// it is possible to write code in this form: f1 = f2 = f3;
// because the return from this method is of type Fraction.
// Also, note the use of "*this". This is the way 
// to return an instance of the current object as a return value


const Fraction& Fraction::operator=(const Fraction& f) {
  if (this != &f) {
    num = f. num;
    denom = f.denom;
  }
  return *this;
}

Fraction::~Fraction() {
}



main.cpp
#include "fraction.h"

#define CAPACITY 10

int main ( ) {

 int nNum;
 int nDenom;
    //int CAPACITY = 10;
    char string[CAPACITY];
    
    Fraction f1;
    Fraction f2;

        cout<<"Test Your Math \n" << endl;
        cout<<"Enter one operation of add, subtract, multiply, equal, quit \n" <<endl;
        cout<<"COMMAND>" <<endl;
        cin>>string;
         
        cin>>nNum;
  cin>>nDenom;

  f1.SetNum( nNum );
  f1.SetDenom( nDenom );

        cout<<f1.GetNum() << endl << f1.GetDenom() << endl;

  cin>>nNum;
  cin>>nDenom;

  f2.SetNum( nNum );
  f2.SetDenom( nDenom );

        cout<<f2.GetNum() << endl << f2.GetDenom() << endl;
        
        switch(strlen(string))
  {
              case 3:
    {
                     Fraction f3=f1+f2;      ////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The sum of Fraction1 & Fraction2 is"<< f3.GetNum() << endl << f3.GetDenom() <<endl;
      break;
    }
                     
         
              case 4:      
                     break;

              case 5:  
      {
                     int f3 = (f1==f2);
                     if(f3 == 1)
      cout<<"The Fraction1 & Fraction2 is same"<<endl;
                     else if(f3 == 0)
      cout<<"The Fraction1 & Fraction2 is same"<<endl;
                     break; 
      }
       
              case 8:
                     if( string[0] = 109){
                     Fraction f3=f1*f2;    /////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The multiply of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;       
                     }
                     else{ 
                     Fraction f3=f1-f2;   /////// error: no matching function for call to ?raction::Fraction(Fraction)[/b]
                     cout<<"The subtract of Fraction1 & Fraction2 is"<<f3.GetNum() << endl << f3.GetDenom() <<endl;       
                     }
                     break;
  }
  return 0;
}



EDIT: when you create projects, create empty projects, to get rid of the "stdafx.h" (I removed it from "fraction.h"), and other files. These are just needed when you want to create a precompiled header.

This post has been edited by sarmanu: 28 February 2010 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1