R32
R32

Reputation: 9

Friend function is not accessing private members of another friend class

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.

Upvotes: 0

Views: 698

Answers (4)

LogicStuff
LogicStuff

Reputation: 19607

exp is not accessible inside the operator+, because friendship is not transitive (wikipedia):

If class A is a friend of class B, and class B is a friend of class C, class A is not automatically a friend of class C.

This applies to friend function too. You have to add the friend declaration to Term too or provide accessor functions.

Upvotes: 2

Deduplicator
Deduplicator

Reputation: 45654

Your free function Polynomial operator+(const Polynomial &a, const Polynomial&b) is not a member of Term so it would be allowed access to the privates, nor a member of it's friend Polynomial (only a friend, and that's not transitive), and thus also doesn't get access to Term's innards that way.

Either make it a member of Polynomial to take advantage of the existing friend-relationship, or preferably make it a friend of Term as well.
There is a sneaky third option, though you probably don't want that, which is defining it inline in Polynomial but keeping it a free function.

Upvotes: 0

Alex Lop.
Alex Lop.

Reputation: 6875

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.

Upvotes: 0

Michael Albers
Michael Albers

Reputation: 3779

The operator+ function isn't actually part of the Polynomial class since it's a friend function.

https://msdn.microsoft.com/en-us/library/465sdshe.aspx

See the "friend functions" section.

Upvotes: 0

Related Questions