mercredi 30 septembre 2015

Can't understand the error with Classes and headerfile:

Error List ////////////////////////////////////////////////

Rational.h: In function ‘void add(const Rational&)’:

Rational.h:8:5: error: ‘int Rational::dem’ is private

int dem; // q ^ Rational.cpp:37:22: error: within this context

 int P = num * h2.dem + h2.num*dem;

                 ^

In file included from Rational.cpp:1:0:

Rational.h:7:5: error: ‘int Rational::num’ is private int num; //p ^ Rational.cpp:37:31: error: within this context int P = num * h2.dem + h2.num*dem; ^ In file included from Rational.cpp:1:0:

Rational.h:8:5: error: ‘int Rational::dem’ is private

int dem; // q

 ^

Rational.cpp:38:20: error: within this context

 int Q = dem*h2.dem;

                ^

Rational.cpp:25:9: warning: unused variable ‘i’ [-Wunused-variable]

 int i = 0;

     ^

Rational.cpp:26:9: warning: unused variable ‘k’ [-Wunused-variable]

 int k = 0;

     ^

Rational.cpp:37:9: warning: unused variable ‘P’ [-Wunused-variable]

 int P = num * h2.dem + h2.num*dem;

     ^

Rational.cpp:38:9: warning: unused variable ‘Q’ [-Wunused-variable]

 int Q = dem*h2.dem;

////////////////////////////////////////////////////////////////////////////////

keep getting error and error. Learning classes and header but not going that great. Can someone explain me these error . header file

#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

.cpp file As you can tell trying different methods for adding but every time i get bench of errors.

void add(const Rational&h2)
{
    /*
        Get the input of the rational numbers
        Put in the equations

        add = ((p/q)


    */
    //int _p, _


   // int  i  , k ;
   // add(h2);
   // i = h2._q;
   // k = h2.dem;
   // num*= k;
   // dem*=k;
 //  num = +h2.num*i;
    int num = 0;
    int dem = 0;
    int i = 0;
    int k = 0;
    add(h2);

   // i = dem;
   // k = h2.dem;
   // num*= k;
   // dem*=k;
  //  num = +h2.num*i;
//    fract (num,dem);


    int P = num * h2.dem + h2.num*dem;
    int Q = dem*h2.dem;

}
void display() const; // _p:_q
{
     if (Q == 1) // e.g. fraction 2/1 will display simply as 2
            cout << P << endl;
        else
            cout <<P << "/" << Q << endl;
}

Aucun commentaire:

Enregistrer un commentaire