I am having trouble getting the compiler to accept my below defined user data type (Term) that accepts another user data type (Rational) as a parameter. Any suggestions on how to make this work would be great!
#ifndef _TERM_H
#define _TERM_H
#include "Rational.h"
using namespace std;
class Term {
public:
//constructors
Term( const Rational &a, const int &b)
{
this->coefficient = a;
this->exponent = b;
}
Term(){}
~Term () {}
//print the Rational
void print()const
{
cout << coefficient << " x^" << exponent << endl;
}
private:
Rational *coefficient, *a;
int exponent, b;
};
#endif
#ifndef _TERM_H
#define _TERM_H
using namespace std;
class Rational {
public:
//constructors
Rational( const int &a, const int &b){
if (a != 0)
if (b != 0)
this->numerator = a;
this->denominator = b;
}
//print the Rational
void print()const {
cout << numerator << "/" << denominator << endl;
}
//add 2 Rationals
void add(const Rational &a, const Rational &b){
numerator = ((a.numerator * b.denominator)+(b.numerator*a.denominator));
denominator = (a.denominator*b.denominator);
}
...
private:
int a, b, numerator, denominator;
};
#endif
I keep getting the below error messages.
Term.h(30) : error C2440: ‘=’ : cannot convert from ‘const Rational’ to ‘Rational *’
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
First, change the definition of
coefficientto this:Then change the constructor to this: