Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to non-static member function without an object argument

Can anyone explain this error any why I am getting it? I believe I've got all of the arithmetic down in my separate class.

This program stores and manipulates fractions, while testing overloading.

Thank you in advance.

I am trying to test this in the driver with the output looking like this.

f1 = 3/6, f2 = 4/5 
f1 + f2 = 39/30 = 13/10 = 1.3 
f1 - f2 = -9/30 = -3/10 = -0.3 
f1 * f2 = 12/30 = 2/5 = 0.4 
f1 / f2 = 15/24 = 5/8 = 0.625 
Enter a fraction [numerator denominator]: 2 5 
You entered 2/5 

My Fraction.cpp class

#include "Fraction.h"

#include <iostream>

using namespace std;

Fraction::Fraction(const int numerator, const int denominator) {  }

int Fraction::getNumerator() const {
    return numerator;
}

int Fraction::getDenominator() const {
    return denominator;
}

int Fraction::gcd() const {
    int n = numerator;
    int d = denominator;
    int temp;

    while (d!=0) {
        temp = n % d;
        n = d;
        d = temp;
    }

    return n;

}

void Fraction::setNumerator(const int numerator) {
    this->numerator = numerator;
}

void Fraction::setDenominator(const int denominator) {
    this->denominator = denominator;
}

string Fraction::toString() const {
    return "";
}

double Fraction::returnDecimal() const {
    double a = (double) getNumerator();
    double b = (double) getDenominator();

    return (a / b);
}

void Fraction::reduce()  {

    int a = gcd();
    int b = numerator / a;
    int c = denominator / a;

    setNumerator(b);
    setDenominator(c);

}

Fraction Fraction::operator +(const Fraction& f) const {

    Fraction temp;

    int a = getNumerator();
    int b = getDenominator();
    int c = f.getNumerator();
    int d = f.getDenominator();

    int tempOne = b;
    int tempTwo = d;

    a = a * tempTwo;
    b = b * tempTwo;

    c = c * tempOne;
    d = d * tempOne;

    temp.setNumerator(a+c);
    temp.setDenominator(d);

    return temp;

}

Fraction Fraction::operator -(const Fraction& f) const {

    Fraction temp;

    int a = getNumerator();
    int b = getDenominator();
    int c = f.getNumerator();
    int d = f.getDenominator();

    int tempOne = b;
    int tempTwo = d;

    a = a * tempTwo;
    b = b * tempTwo;

    c = c * tempOne;
    d = d * tempOne;

    temp.setNumerator(a-c);
    temp.setDenominator(d);

    return temp;

}

Fraction Fraction::operator *(const Fraction& f) const {

    Fraction temp;

    temp.setNumerator(getNumerator() * f.getNumerator());
    temp.setDenominator(getDenominator() * f.getDenominator());

    return temp;

}

Fraction Fraction::operator /(const Fraction& f) const {

    Fraction temp;

    temp.setNumerator(getNumerator() * f.getDenominator());
    temp.setDenominator(getDenominator() * f.getNumerator());

    return temp;

}

bool Fraction::operator ==(const Fraction& f) const {

    return ( (getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator()) );

}

bool Fraction::operator !=(const Fraction& f) const {

    return !( (getNumerator() == f.getNumerator()) && (getDenominator() == f.getDenominator()) );

}

bool Fraction::operator <(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) < (c/d));

}

bool Fraction::operator <=(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) <= (c/d));

}

bool Fraction::operator >(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) > (c/d));

}

bool Fraction::operator >=(const Fraction& f) const {

    double a = getNumerator();
    double b = getDenominator();

    double c = getNumerator();
    double d = getDenominator();

    return ((a/b) >= (c/d));

}

ostream& operator <<(ostream& out, const Fraction& f) {
    out << f.getNumerator() << "/" << f.getDenominator();

    return out;
}

istream& operator >>(istream& in, Fraction& f) {    

    int a;
    int b;

    in >> a >> b;

    f.setNumerator(a);
    f.setDenominator(b);

    return in;

}

My main class.

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

int main(int argc, const char * argv[])
{

    Fraction::getNumerator() const;
    Fraction::getDenominator() const;
    Fraction::gcd() const;
    Fraction::setNumerator(<#const int numerator#>);
    Fraction::setDenominator(<#const int denominator#>);
    Fraction::toString();
    Fraction::returnDecimal() const;
    Fraction::reduce();

    return 0;
}

Fraction.h

#include <iostream>
#include <string>

using namespace std;

class Fraction {

private:

    int numerator;

    int denominator;

    int gcd() const;

public:

    Fraction(const int numerator = 0, const int denominator = 0);

    int getNumerator() const;

    int getDenominator() const;

    void setNumerator(const int numerator);

    void setDenominator(const int denominator);

    string toString() const;

    double returnDecimal() const;

    void reduce();

    bool operator ==(const Fraction& f) const;

    bool operator !=(const Fraction& f) const;

    bool operator <(const Fraction& f) const;

    bool operator <=(const Fraction& f) const;

    bool operator >(const Fraction& f) const;

    bool operator >=(const Fraction& f) const;

    Fraction operator +(const Fraction& f) const;

    Fraction operator -(const Fraction& f) const;

    Fraction operator *(const Fraction& f) const;

    Fraction operator /(const Fraction& f) const;

    friend ostream& operator <<(ostream&, const Fraction&);

    friend istream& operator >>(istream&, Fraction&);

};
like image 348
MatthewTingle Avatar asked Oct 03 '14 03:10

MatthewTingle


People also ask

Which member function can be called without creating any object?

Explanation: The member functions can be called using only the dot operator or the arrow operator. But the static members can be called using directly the class name followed by the scope resolution operator and static member function name. This is useful when you don't have any object to call the member.

Can we call any class member function without using object of the class?

Like static data members, you may access a static member function f() of a class A without using an object of class A .

How do you call a non-static member of a class?

Non-Static Data Members They are declared using the keyword 'static'. Every member in java defaults to a non-static without a 'static' keyword preceding it. All objects of a class share the same copy of static data members. Each object of the class gets its own copy of Non-Static data members.

Can a non-static function call static function?

A static method provides NO reference to an instance of its class (it is a class method) hence, no, you cannot call a non-static method inside a static one.


1 Answers

Fraction is a class. You need an object of that class in order to call its (nonstatic) functions.

Fraction f;
f.getNumerator();
like image 103
ScottMcP-MVP Avatar answered Sep 21 '22 13:09

ScottMcP-MVP