Hello fellows,
I want to access protected member of inner class from within friend function of the outer class, without adding getter to inner class. Is that possible ? Any advice ?
Code:
#include <iostream>
#include <string>
#include <cmath>
class Polynomial {
protected:
class Term {
protected:
int exponent;
int coefficient;
Term *next;
Term(int exp, int coeff, Term *n)
{
exponent = exp;
coefficient = coeff;
next = n;
}
friend class Polynomial;
};
private:
Term * start;
public:
Polynomial() { start = NULL; }
// ...
friend ostream & operator << (ostream &out, const Polynomial &p);
};
ostream & operator << (ostream &out, const Polynomial &p)
{
Polynomial::Term *temp = p.start;
while (temp != NULL)
{
if (temp->coefficient < 0)
out << "- ";
else
out << "+ ";
if (temp->coefficient != 1)
out << abs(temp->coefficient);
if (temp->exponent == 1)
out << "x";
else
out << "x^" << temp->exponent;
temp = temp->next;
}
return out;
}
Red parts are not recognised.
Thanks in advance.