c++ - Need help Fixing these errors with classes -


i need understanding these error. have been trying figure out can't working. algorithm adding right?

here current error:

'dem' not declared in scope.

i thought header file takes care of initialization.

rational.h

#ifndef _rational_h_ #define _rational_h_ #include <iostream> using namespace std; class rational {       int num; //p     int dem; // q   public:  rational();  rational(int p, int q = 1);  void display() const; // _p:_q  void add(const rational&);  }; #endif 

rational.cpp

#include "rational.h" int main()   {      rational r1(1 ,2);     rational r2(1,4);     r1.add(r2);     r1.display();   } void add(const rational&h2) {      int  i, k;     rational fract;     add(h2);      = dem;     k = h2.dem;     num*= k;     dem*=k;     num = +r2.num*i;     //return  } 

you're defining add() global free function, not member function of class rational. can't access member variable dem in it.

change

void add(const rational&h2) { ... 

to

void rational::add(const rational&h2) { ... 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -