Hello, hopefully someone would be able to help me with this project I am working on. I have two classes now RationalNumber and Term. The RationalNumber class works fine when I invoke it directly to create a fraction. The Term class was created to leverage the RationalNumber class to create at term that would look like this 1/2x^2. I'm having issues trying to get the output working. I realize the standard << operator is able to process ints, doubles, etc,. How would I go about coding an appropriate << term?

The Error I receive is "error: no match for 'operator<<' in 'std::cout << RationalNumber(((const RationalNumber&)((const RationalNumber*)(+ it. __gnu_debug::_Safe_iterator<_Iterator, _Sequence>::operator* [with _Iterator = __gnu_cxx::__normal_iterator<RationalNumber*, __gnu_norm::vector<RationalNumber, std::allocator<RationalNumber> > >, _Sequence = __gnu_debug_def::vector<RationalNumber, std::allocator<RationalNumber> >]()))))'"

Thank you in advance!

#include <iostream>
using namespace std;

class RationalNumber {
public:
	//initializing Constructors/functions
	RationalNumber();
	RationalNumber(int, int);
	void initRational();
	void setNumerator(int);
	int getNumerator();
	void setDenominator(int);
	int getDenominator();
	int getGCD();
	void simplifyRational(int);
	void print();
	
	//Overloaded operators
	RationalNumber operator+(RationalNumber) const; 
	RationalNumber operator-(RationalNumber) const;
	RationalNumber operator*(RationalNumber) const;
	RationalNumber operator/(RationalNumber) const;
	friend ostream &operator << (ostream &output, RationalNumber &fraction) {
		return fraction.display(output);
	};
	
private:
	//declaring private variables
	int numerator;
	int denominator;	
	virtual ostream &display(ostream &output) {
		output << numerator << denominator;
		return output;
	};
};
#include "RationalNumber.h"
#include <iostream>
using namespace std;

//Empty Constructor
RationalNumber::RationalNumber() {
	
};

//Rational Constructor
RationalNumber::RationalNumber(int num, int den) {
	if (num != 0)
		if (den !=0)
			numerator = num;
	denominator = den;
}

void RationalNumber::initRational() {
	char slash;
	cout << "\nPlease enter a rational number in the form of a/b: ";
	cin >> numerator >> slash >> denominator;
	if (denominator == 0) {
		cout << "\nCannot divide by 0, please enter another denominator: ";
		cin >> denominator;
	}
}

//sets the numerator for the rational
void RationalNumber::setNumerator(int num) {
	numerator = num;
}

//obtains the value of the numerator
int RationalNumber::getNumerator() {
	return numerator;
}

//sets the denominator for the rational
void RationalNumber::setDenominator(int den) {
	denominator = den;
}

//obtains the value of the denominator
int RationalNumber::getDenominator() {
	return denominator;
}

//Obtains the greatest commin divisor
int RationalNumber::getGCD() {
	int temp;
	int num = numerator;
	int den = denominator;
	while (den != 0) {
		temp = den;
		den = num % den;
		num = temp;
	}
	return num;
}

void RationalNumber::simplifyRational(int gcd) {
	numerator = numerator / gcd;
	denominator = denominator / gcd;
}

//Display Rational number
void RationalNumber::print() {
	cout << numerator << "/" << denominator << endl;
}

RationalNumber RationalNumber::operator+(RationalNumber rationalptr) const {
	RationalNumber ptr;
	ptr.numerator = numerator * rationalptr.denominator + denominator * rationalptr.numerator;
	ptr.denominator = denominator * rationalptr.denominator;
	return(ptr);
}

RationalNumber RationalNumber::operator-(RationalNumber rationalptr) const {
	RationalNumber ptr;
	ptr.numerator = numerator * rationalptr.denominator - denominator * rationalptr.numerator;
	ptr.denominator = denominator * rationalptr.denominator;
	return(ptr);
}

RationalNumber RationalNumber::operator*(RationalNumber rationalptr) const {
	RationalNumber ptr; 
	ptr.numerator = numerator * rationalptr.numerator;
	ptr.denominator = denominator * rationalptr.denominator;
	return(ptr);
}

RationalNumber RationalNumber::operator/(RationalNumber rationalptr) const {
	RationalNumber ptr;
	ptr.numerator = numerator * rationalptr.denominator;
	ptr.denominator = denominator * rationalptr.numerator;
	return(ptr);
}
#include "RationalNumber.h"
#include <iostream>
#include <vector>
using namespace std;

class Term : public RationalNumber  {
public:
	Term();
	void initExponent();
	void setExponent(int);
	int getExponent();
	void prepTerm();
	void displayTerm();
	virtual void print();
	
private:
	int exponent;
	vector<RationalNumber> rational_vector;
	virtual ostream &display(ostream &output) {
		return output;
	}
};
#include "Term.h"
#include <iostream>
using namespace std;

Term::Term() {
	rational_vector.reserve(3);
}
void Term::initExponent() {
	cout << "Enter the exponent: ";
	cin >> exponent;
}

void Term::setExponent(int exp) {
	exponent = exp;
}
int Term::getExponent() {
	return exponent;
}

void Term::prepTerm() {
	RationalNumber rational;
	rational.initRational();
	rational_vector.push_back(rational);

	cout << "Enter the exponent: ";
	cin >> exponent;
}

void Term::print() {
//	RationalNumber::print();
//	cout << rational_vector[0];
//	cout << rational.numerator << "/" << rational.denominator << "x^" << getExponent();
	cout << "x^" << getExponent();
}

void Term::displayTerm() {
	vector<RationalNumber>::iterator it;
	
	for (it=rational_vector.begin(); it!=rational_vector.end(); ++it) {
		cout << ((RationalNumber)*it) << endl;
	}
}
This article has been dead for over six months. Start a new discussion instead.