I have two classes Term and Polynomial. Polynomial class is declared to be a friend of Term class. Polynomial class has friend function in it. When i implement that function in a cpp file of Polynomial class, the private members of Term are not getting accessed there. Although the Polynomial class is declared to be its friend. All i need is to have access private members of Term in that function. Any help? Here is my code:
Polynomial.h file:
#pragma once
#include "Term.h"
using namespace std;
// class polynomial;
// friend operator+(const polynomial& , const polynomial&);
class Polynomial
{
private:
Term *termArray;
int capacity;
int terms;
public:
Polynomial();
Polynomial(int, int);
friend Polynomial operator+(const Polynomial& , const Polynomial&);
};
Here is my Term.h:
#pragma once
using namespace std;
class Polynomial;
class Term
{
friend Polynomial;
private:
int exp;
float coef;
};
Here is my Polynomial.cpp file:
#include <iostream>
#include "Polynomial.h"
#include "Term.h"
using namespace std;
Polynomial::Polynomial()
{
capacity = 1;
terms = 0;
}
Polynomial::Polynomial(int cap, int noOfTerms)
{
capacity = cap;
terms = noOfTerms;
termArray = new Term[terms];
}
Polynomial operator+(const Polynomial &a, const Polynomial &b)
{
for(int i = 0; i<a.terms; i++)
for(int j=0; j < b.terms; j++)
if(a.termArray[i].exp == b.termArray[j].exp)
{
//something to do here.
}
}
The error that i am getting is at "exp" within if condition of overloaded function of + that it is inaccessible.
Polynomial operator+(const Polynomial &a, const Polynomial&b)
is trying to access the private members of Term
not Polynomial
class.
So Polynomial operator+(const Polynomial &a, const Polynomial&b)
has to be a friend of Term
too.