Thread: no match for "operator+" problem!

1. no match for "operator+" problem!

Hello everyone, Im writing a fraction program that will do some nifty stuff. To cut to the chase, here are my 3 files but Im having trouble compiling the test function, i keep getting a "no match for 'operator+'" error. Thanks for all and any help!

fraction.h

Code:
```#ifndef Fraction
#define Fraction

class mixedfraction
{
public:
mixedfraction();
void display();
double evaluate();
mixedfraction sub (mixedfraction);
mixedfraction mul (mixedfraction);
mixedfraction div (mixedfraction);
int whole;
int numerator;
int denominator;
};
#endif```

fraction.cpp

Code:
```#include <iostream>
#include <cmath>
#include "mixedfraction.h"

using namespace std;

mixedfraction::mixedfraction()
{
whole=0;
numerator=0;
denominator=1;
}

{
cout << "Please enter whole number: " <<endl;
cin >> whole;
cout << "Please enter numerator: " <<endl;
cin >> numerator;
cout << "Please enter denominator: " <<endl;
cin >> denominator;
}

void mixedfraction::display()
{
cout << whole << numerator <<"/" << denominator << endl;
}

double mixedfraction::evaluate()
{
double temp;
temp = (double) whole + (double) numerator / (double) denominator;
return temp;
}

mixedfraction add (mixedfraction f1, mixedfraction f2)
{
mixedfraction sum;
sum.numerator = f1.numerator*f2.denominator + f2.numerator*f1.denominator;
sum.denominator = f1.denominator*f2.denominator;
return sum;
}```

test.cpp

Code:
```#include <iostream>
#include <cmath>
#include "mixedfraction.h"

using namespace std;

int main()
{
mixedfraction x,y,z;
double i;
x.display();
i = x.evaluate();
cout << i << endl;
y.display();
z= x+y;
cout << "The sum of ";
x.display();
cout << "and";
y.display();
cout << "is";
z.display();
}```

2. You are getting that error because you don't provide a + operator in your class. So you can provide one or change the
Code:
`z = x + y`
To
Code:
`z = x.add(x, y);`
Also I would like to note. That I would change that add definition to actually operate on the class itself and only take one parameter

3. add should be like all the other methods you declared. Taking one argument makes more sense. Also, since this is c++ you should take advantage of operator overloading.

4. thanks for the help guys!

changing it to "z = x.add(x, y);" worked. However, when I removed the second parameter, I get a compiling error:
mixedfractiontest.cpp:17: error: no matching function for call to ‘mixedfraction::add(mixedfraction&, mixedfraction&)’

Im sure I misunderstood what you guys were saying.

EDIT: when i try to compile the files together, I get this error:

mixedfractiontest.o: In function `main':
/home/atayh/Programs/mixedfractiontest.cpp:17: undefined reference to `mixedfraction::add(mixedfraction, mixedfraction)'
collect2: ld returned 1 exit status

5. Originally Posted by wankel
thanks for the help guys!

changing it to "z = x.add(x, y);" worked. However, when I removed the second parameter, I get a compiling error:
mixedfractiontest.cpp:17: error: no matching function for call to ‘mixedfraction::add(mixedfraction&, mixedfraction&)’

Im sure I misunderstood what you guys were saying.
It should be x.add(y), if you want to add y to x.

6. Originally Posted by tabstop
It should be x.add(y), if you want to add y to x.
thanks tabstop. I made your change and both the fraction.cpp and fractiontest.cpp compile fine individually. But when I try to combine them using the following line on linux:

g++ -Wall -ggdb mixedfractiontest.o mixedfraction.o

I get this error:

mixedfractiontest.o: In function `main':
collect2: ld returned 1 exit status

Im not sure what the problem could be since they compile just fine seperately =/

7. You have declared it as a member function but implemented it as a free function.

8. I would also like to give you a tip: SourceForge.net: Do not remove parameter names - cpwiki

9. Thanks for the help and advice everyone! I finally got everything working nicely, but there is one last thing I would like some help on.

At the end of "test.cpp", how do I make z.display() the new x.read()? I have my program set up in a loop so when I get the sum of the addition, id like to use that new number as my default. I tried "x.read()=z.display()", but I got an error saying thats an invalid use of void. Is there another way to do this? Any help/tips/advice would be very much appreciated!