Passing class name as arguments to member functions

I'm having issues with the last part of my code. I have to add the declaration of void multiply(Fraction,Fraction); to my Fraction class, but when doing so, I get this error in my class implementation file: error: no 'void Fraction::multiply(Fraction, Fraction)' member function declared in class 'Fraction'.

Before this, my program executed fine. Other than adding that member function, nothing else has been altered.

Here is my specification file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
//Fraction class declaration
class Fraction
{
    private:
        int numerator;
        int denominator;
        
    public:
        Fraction();
        Fraction(int,int);
        void setNumerator(int);
        void setDenominator(int);
        int getNumerator() const;
        int getDenominator() const;
        void print();
        void multiply(Fraction,Fraction);
};
#endif // FRACTION_H_INCLUDED 


Here is the implementation file. We aren't using inline member functions for this program, so I didn't do that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "Fraction.h"
#include <iostream>
using namespace std;

//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
    numerator = 0;
    denominator = 0;
}

//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
    numerator = num;
    denominator = den;
}

//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
    numerator = n;
}

//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
    denominator = d;
}

//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
    return numerator;
}

//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
    return denominator;
}

//print displays the fractions
void Fraction::print()
{
    cout<<numerator<<"/"<<denominator;
}

void Fraction::multiply(Fraction fract1,Fraction fract2)
{
    fract1*fract2;
}


I can't seem to see where my problem is. I've searched for the answer and I see a lot of people talk about templates (I think for the same situation), but we haven't covered anything like that. Help would be appreciated. Everything was going fine until this step.
On line 51 you are trying to multiply two fraction objects w/o overloading the * operator for class Fraction first - the error I'm getting is therefore:

error: no match for 'operator*' (operand types are 'Fraction' and 'Fraction') ..

So try overloading operator * first for class Fraction
So, you're not getting an error on line 49? That's where I'm getting my error. It hasn't made it to line 51. I'll read on how to overload operators, since I haven't gotten that far yet.
There are a number of things wrong with your multiply function.

1) You've declared a void function. You want to return the resulting fraction as the value of the function. You don't store the result anywhere.

2) As GunnerFunner mentioned, C++ doesn't know how to multiply a custom class. You don't necessarily have to overlaod the * operator. Your multiply function is usable, but you need to fix your arithmetic.
1
2
3
4
5
6
7
Fraction Fraction::multiply (Fraction fract1,Fraction fract2)  // Note return type
{  Fraction rslt;

    rslt.numerator = fract1.numerator * fract2.numerator;
    rslt.denominator = fract1.denominator * fract2.denominator;
    return rslt;
}


The conventional C++ way of doing this is to overload the * operator.
1
2
3
4
5
6
7
8
Fraction Fraction::operator * (Fraction fract2)  // fract1 is implied (this)
{  Fraction rslt;

    rslt.numerator = numerator * fract2.numerator;
    rslt.denominator = denominator * fract2.denominator;
    //  You may want to reduce the result at this point.
    return rslt;
}


1) You've declared a void function. You want to return the resulting fraction as the value of the function. You don't store the result anywhere.


Per the instructions, the function needs to remain void, so I guess I'd have to overload the * operator? I was in the middle of doing that, but I see I've made some mistakes.

Actually no, I did it correctly. I coded what you wrote, but do I also need to add rslt.multiply(Fraction fract1,Fraction fract2);? Looking at the example in my textbook, that's what they did.

With or without that statement, I'm still getting the same error for line 49, only this time with the overload function.
Last edited on
Per the instructions, the function needs to remain void

Then how are you supposed to return the result?

but do I also need to add rslt.multiply(Fraction fract1,Fraction fract2);?

You can, but it's not necessary. Either snippet I posted will do the job.

Is this what you're looking for?
1
2
3
4
5
6
7
8
9
//  Will place the result in this
void Fraction::multiply (Fraction fract1,Fraction fract2)  
{  numerator = fract1.numerator * fract2.numerator;
    denominator = fract1.denominator * fract2.denominator;
}

//  This would be invoked as 
  Fraction f1, f2, f3;    //  Initialize f1, f2 somehow
  f3.multiply (f1, f2);  // result is placed in f3 


I'm still getting the same error for line 49, only this time with the overload function.

I can't duplicate that error with your posted code. I get the same error as GunnerFunner. Are you sure the code you posted matches what you're compiling.
Last edited on
Ah, the dot operator has slipped passed me...again. I'm still getting that same error for line 49.
I can't duplicate that error with your posted code. I get the same error as GunnerFunner. Are you sure the code you posted matches what you're compiling.

Yes, I'm posting the same code.

Here is what my main function looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include "Fraction.h"
using namespace std;

int main()
{
    Fraction f1;
    Fraction f2(1,4);
    Fraction f3;

    int num, den;

    cout<<"Please enter a value for the numerator: ";
    cin>>num;
    cout<<"Please enter a value for the denominator: ";
    cin>>den;

    f1.setNumerator(num);
    f1.setDenominator(den);
    f3.multiply(f1,f2);

    cout<<"Fraction 1 is ";
    f1.print();
    cout<<endl;
    cout<<"Fraction 2 is ";
    f2.print();
    cout<<endl;
    cout<<"Fraction 1 times Fraction 2 is ";
    f3.print();
    return 0;
}


Now, when removing everything that has to do with the multiply function, the program executes.
Last edited on
Line 20: The void function makes sense now. Would have clarified things if you had posted main in your OP. The latest snippet I posted should work for you.

If that snippet doesn't work, please post your current fraction.h and fraction.cpp again please.

Sorry, it didn't cross my mind until now.

The Fraction.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
//Fraction class declaration
class Fraction
{
    private:
        int numerator;
        int denominator;

    public:
        Fraction();
        Fraction(int,int);
        void setNumerator(int);
        void setDenominator(int);
        int getNumerator() const;
        int getDenominator() const;
        void print();
        void multiply(Fraction,Fraction);
};
#endif // FRACTION_H_INCLUDED 


The Fraction.cpp file where I'm still getting the error on line 49:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "Fraction.h"
#include <iostream>
using namespace std;

//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
    numerator = 0;
    denominator = 0;
}

//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
    numerator = num;
    denominator = den;
}

//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
    numerator = n;
}

//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
    denominator = d;
}

//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
    return numerator;
}

//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
    return denominator;
}

//print displays the fractions
void Fraction::print()
{
    cout<<numerator<<"/"<<denominator;
}

void Fraction::multiply(Fraction fract1,Fraction fract2)
{
    numerator=fract1.numerator*fract2.numerator;
    denominator=fract1.denominator*fract2.denominator;
}
I get no error when compiling fraction.cpp with VS2015.

What compiler are you using?

How do I look to see? I'm using CodeBlocks.

I've sent my files to my professor to see if he can tell what's going wrong. I believe he's using CodeBlocks as well.
Last edited on
Ah, I finally found the problem from googling. There was a Fraction.h.gch file in the same folder as my project. I moved it and my program now runs.

What is this file?
Oh, yeah. When I first linked the files, I accidentally said to compile the .h file.

Thanks for your help.
Started overloading functions on the next assignment. I understand what's going on, but I don't understand why the output is displaying the wrong sum. I keep getting 4/9 when it should be 19/20. I've set up my overloaded functions based off what was written in the textbook. I get the correct sum for f3, but for f4, I don't.

.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef FRACTION_H_INCLUDED
#define FRACTION_H_INCLUDED
//Fraction class declaration
class Fraction
{
    private:
        int numerator;
        int denominator;

    public:
        Fraction();
        Fraction(int,int);
        void setNumerator(int);
        void setDenominator(int);
        int getNumerator() const;
        int getDenominator() const;
        void print();
        Fraction operator*(Fraction);
        Fraction operator+(Fraction);
};
#endif // FRACTION_H_INCLUDED 


.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Fraction.h"
#include <iostream>
using namespace std;

//Constructor initializes the numerator and denominator
Fraction::Fraction()
{
    numerator = 0;
    denominator = 0;
}

//Constructor accepts arguments for numerator and denominator
Fraction::Fraction(int num,int den)
{
    numerator = num;
    denominator = den;
}

//setNumerator assigns a value to the numerator member
void Fraction::setNumerator(int n)
{
    numerator = n;
}

//setDenominator assigns a value to the denominator member
void Fraction::setDenominator(int d)
{
    denominator = d;
}

//getNumerator returns the value in the numerator member
int Fraction::getNumerator() const
{
    return numerator;
}

//getDenominator returns the value in the denominator member
int Fraction::getDenominator() const
{
    return denominator;
}

//print displays the fractions
void Fraction::print()
{
    cout<<numerator<<"/"<<denominator;
}

Fraction Fraction::operator*(Fraction fract2)  // fract1 is implied (this)
{
    Fraction temp;

    temp.numerator=numerator*fract2.numerator;
    temp.denominator=denominator*fract2.denominator;
    //  You may want to reduce the result at this point.
    return temp;
}

Fraction Fraction::operator+(Fraction fract2)
{
    Fraction temp;

    temp.numerator=numerator+fract2.numerator;
    temp.denominator=denominator+fract2.denominator;
    return temp;
}


main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "Fraction.h"
#include <iostream>
using namespace std;

int main()
{
    Fraction f1(1,5);
    Fraction f2(3,4);
    Fraction f3;
    Fraction f4;

    f3=f1*f2;
    f4=f1+f2;

    cout<<"f3 = ";
    f3.print();

    cout<<"\n\nf4 = ";
    f4.print();
Check your addition formula,

a/b + c/d = (a.d + c.b) / (b + d)
Are you saying that's what I need to do? That would give the wrong answer as well.
1/5 + 3/4 = (4 + 15) / 9 = 19 / 9, or am I not seeing it correctly?

I need to do (a(d) + c(b)) / (b * d)

I was following the instructions for the assignment and it also shows the exact same code for overloading the + operator in the book. So, will I need to alter it for my needs? It's just not going to be the same every time.

Edit: Yeah, that's what tripped me up. I was thinking the formula was the same no matter what. This worked.
1
2
3
4
5
6
7
8
Fraction Fraction::operator+(Fraction fract2)
{
    Fraction temp;

    temp.numerator=(numerator*fract2.denominator)+(denominator*fract2.numerator);
    temp.denominator=denominator*fract2.denominator;
    return temp;
}
Last edited on
Topic archived. No new replies allowed.