2

I am trying to create an overloaded friend function that will add two fractions. So far I have the following solution but I have errors at line 17 and 19. I believe the problem is with the parameters (fraction amount1, fraction amount2) but I can't exactly put my finger on it. Can anyone help? This needs to compile on Visual Studio 2010.

#include <iostream>

using namespace std;

class fraction
{
public:
fraction(); //constructor for initialize
void read(); //read 3 int from keyboard for the fraction
void print(); //print the fraction on screen
friend fraction operator +(fraction, fraction); //fraction add
private:
int integral, numerator, denominator;
}


int main(){

return 0;
}

fraction operator +(fraction amount1, fraction amount2)
{
fraction temp;
temp.integral = amount1.integral + amount2.integral;
temp.numerator = amount1.numerator + amount2.numerator;
temp.denominator = amount1.denominator + amount2.denominator;
return temp;
}


void fraction::read()
{
cin >> integral >> numerator >> denominator;
}

EDIT: I have corrected my addition algorithm to

friend fraction operator +(const fraction&, const fraction&); //fraction add

and

fraction operator +(const fraction& amount1, const fraction& amount2)
{
fraction temp;
temp.integral = amount1.integral + amount2.integral;
temp.numerator = (amount1.numerator)*(amount2.denominator) + (amount2.numerator)*(amount1.denominator);
temp.denominator = (amount1.denominator)*(amount2.denominator);
return temp;
}

However on testing this I output zeros for the numerator and denominator variables. Can anyone help with this?

user1905552
  • 101
  • 2
  • 7
  • 1
    See [here](http://stackoverflow.com/questions/4421706/operator-overloading) for good advice on how to overload each operator. For example, `operator+` is usually implemented in terms of `operator+=`. – chris Dec 15 '12 at 02:05
  • 1
    You probably want your `operator +` to take `const` references. – David Schwartz Dec 15 '12 at 02:08
  • 1
    You miss a `;` after class definition. Otherwise, your code is perfectly fine (as far as it "compilability" is concerned). A lot can be improved in it, but there are no errors in it besides that `;`. – AnT stands with Russia Dec 15 '12 at 02:10
  • "I can't exactly put my finger on it", it would help if you included the compiler error(s) in your question. – johnsyweb Dec 15 '12 at 02:12
  • Thanks, can't believe I missed that semicolon. Unfortunately I now have a new error about an unresolved external that I have no idea how to solve. – user1905552 Dec 15 '12 at 02:16
  • 1
    **Please include the error message(s).** Assuming this is your entire code, you're missing a semicolon after the class definition which accounts for the compilation errors. Furthermore, you declared a constructor but didn't defined it which will cause a linker error. The same goes for `print()` but since this method is not called in your code, it does not cause an error (yet). – Anonymous Coward Dec 15 '12 at 02:19
  • the exact error is LNK2019: unresolved external symbol "public:_thiscall fraction::fraction(void)" (?? 0fraction@@QAE@XZ) referenced in function "class fraction_cdecl operator+(fraction amount1, fraction amount2)" (??H@YA?AVfraction@@V0@0@Z) can anyone help? – user1905552 Dec 15 '12 at 02:24
  • Thanks Anonymous Coward, yeah I wasn't sure how much code to give in my question as it's still a work in progress. I figured more was better than less. – user1905552 Dec 15 '12 at 02:27
  • Your linker error is, as Anonymous Coward stated, that you declared a default constructor but have yet to define it. – johnsyweb Dec 15 '12 at 02:34
  • 1
    Note that your addition algorithm is wrong. `1/2 + 1/2 != 2/4`. – Daniel Fischer Dec 15 '12 at 02:56
  • Daniel Fischer, yes just realised this and am trying to rectify it however temp.numerator = (amount1.numerator)*(amount2.denominator) + (amount2.numerator)*(amount1.denominator); just gives me a zero when I test it. Can anyone help with this? – user1905552 Dec 15 '12 at 04:27

3 Answers3

0

This compiles after a copule of changes:

  • You need to add a semicolon after the class definition, and
  • You need to define the constructor that you have declared, i.e.

this constructor

fraction(); //constructor for initialize

should have a body defined either inline, or in a separate file.

Additionally, you should change the signature of your operator to

friend fraction operator +(const fraction&, const fraction&)
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0
#include <iostream>

using namespace std;

class fraction
{
public:
//fraction(); //constructor for initialize
void read(); //read 3 int from keyboard for the fraction
void print(); //print the fraction on screen
friend fraction operator +(fraction, fraction); //fraction add
private:
int integral, numerator, denominator;
};


int main(){
fraction f1, f2, f3;
cout<<"\n Enter first fraction: ";
f1.read();
cout<<"\n Enter second fraction: ";
f2.read();
cout<<"\n result: ";

f3 = f1 + f2;
f3.print();
return 0;
}

fraction operator +(fraction amount1, fraction amount2)
{
fraction temp;
temp.integral = amount1.integral + amount2.integral;
temp.numerator = amount1.numerator + amount2.numerator;
temp.denominator = amount1.denominator + amount2.denominator;
return temp;
}


void fraction::read()
{
cin >> integral >> numerator >> denominator;
}

void fraction::print()
{
cout << integral<<" "<< numerator <<" "<< denominator <<std::endl;
}
Neilkantha
  • 91
  • 6
-1

You need the class name before the operator if defined outside of the class block:

fraction fraction::operator +(fraction amount1, fraction amount2);
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156