Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Why does the: ''call to non-static member function without an object argument'' error occur?
Next
Answered
Cecil Lee
  • 23
Cecil Lee
Asked: May 18, 20222022-05-18T18:37:57+00:00 2022-05-18T18:37:57+00:00In: cpp

Why does the: ”call to non-static member function without an object argument” error occur?

  • 23

. Advertisement .

..3..

. Advertisement .

..4..

How to solve the problem – call to non-static member function without an object argument? I have the sample detail:

The output looking like:
 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
#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;
 
 }
#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;
 }
#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&);
 
 };

While I was running it, I found the warning message:

call to non-static member function without an object argument

That is my question in my midterm exam, and it is urgent. I searched the solutions on some websites, but I didn’t get it. I may miss any line or other changes. I appreciate your assistance!

object
  • 2 2 Answers
  • 90 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-16T02:03:03+00:00Added an answer on June 16, 2022 at 2:03 am

    The cause:

    You got this error because you are trying to access the functions of a class but you don’t instantiate it.

    Solution:

    Fraction is a class, so before using its (not static) functions, you need create an object of that class.

    You can do as following:

    Fraction f; f.getNumerator();
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Gabriel Pons
    2022-05-25T20:47:37+00:00Added an answer on May 25, 2022 at 8:47 pm

    Fraction is a type of class. To call the nonstatic functions of this class, you need an object from that class.

    Fraction f;
    f.getNumerator();
    • 18
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.