Question

I could use some help!

This is my first course in any sort of coding and I've hit my first major wall, this is with C++ classes. My problem occurs in line 19 of main.cpp:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

My problem is as simple as that, I need to find out what's keeping my multipliedBy function from being an applicable operand so I can carry on with my programming!

On a side note, if it is incorrect do not worry about my "result" function, as I'm still in the process of tinkering around with that.

Thank you for your time.

fraction.cpp

#include <iostream>
#include "fraction.h"
using namespace std;

fraction::fraction()
{
    top = 0; //default constructor 
    bottom = 1;
}

fraction::fraction(int numerator, int denominator)
{
    top = numerator;
    bottom = denominator;
}

void fraction::answer()
{
    top = numAnswer;
    bottom = denAnswer;
}

void fraction::print() const
{
    cout << top << "/" << bottom; 
}

void fraction::multipliedBy(fraction f2)
{
    numAnswer = top * f2.top;
    denAnswer = bottom * f2.bottom;
}

void fraction::result(fraction answer)
{
    cout << " is ";
}

fraction.h

#include <iostream>

#ifndef FRACTION_H
#define FRACTION_H
using namespace std;

class fraction {
    public:
        fraction();
        fraction(int numerator, int denominator);
        void answer();
        void print() const;
        void multipliedBy(fraction f2);
        void result(fraction answer);
    private:
        int numerator;
        int denominator;
        int top;
        int bottom; 
        int numAnswer;
        int denAnswer;
};

#endif

main.cpp

#include <iostream>
#include "fraction.h"
using namespace std;

int main()
{
    fraction f1(9,8);
    fraction f2(2,3);
    fraction result;

    cout << "The result starts off at ";
    result.print();
    cout << endl;

    cout << "The product of ";
    f1.print();
    cout << " and ";
    f2.print();
    result = f1.multipliedBy(f2);
    result.print();
    cout << endl;
}
Was it helpful?

Solution

multipliedBy() needs to be something like this:

fraction fraction::multipliedBy(fraction f2)
{
    fraction theAnswer(top, bottom);
    theAnswer.top *= f2.top;
    theAnswer.bottom *= f2.bottom;
    return theAnswer;
}

You'll need to change the header too. Note that this solution doesn't modify the fraction you are working on or f2. It just returns a new fraction with the answer in it.

You need (at least) a comment explaining why you have all of these members. Seems you only need a top and a bottom (whatever you call them).

  • numerator
  • denominator
  • top
  • bottom
  • numAnswer
  • denAnswer
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top