Question

Sorry if the question itself seems a bit confusing this is my code for my cpp file.

//main.cpp

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

 int unitTests(Fraction* f) {

 cout << "Fraction Simplifier Tests" << endl;
 cout << "=========================" << endl;

 // fill in code for unit tests


    int num, den;

    Fraction::set(4,16);

    cout << "Input:  " << num << "/" << den << endl;
    cout << "Output: " << num << "/" << den << endl;

 }

 int main() {
 Fraction fraction;
 bool passed;

 passed = unitTests(&fraction);

 if (passed)
     cout << "Passed All Tests" << endl;
 else
     cout << "Failed Tests" << endl;
 }

This is my .h file

class Fraction {
  int n, d;

public:
  void set(int n, int d);
  void simplify();
  void display() const;
  int numerator() const;
  int denominator() const;
};

I have another .cpp file where i store the code for my member functions, but im not having any issue with that so i wont include it.

Basically im getting the error that says I cannot call a member function from void Fraction::set(int, int) without an object. I am unsure how i would go about doing this... I have changed the line to Fraction::fraction.set(4,16) in my confusion. If anyone could shed some light on how I would go about how I can use my fraction object here it would be much appreciated.

Was it helpful?

Solution

This is just that your "call a member function" syntax is wrong; the rest of the code and the question is all besides the point.

When you wrote:

Fraction::set(4,16);

You meant:

f->set(4,16);

There are other problems, like your int num, den have no values and don't do anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top