bikrathor
bikrathor

Reputation: 96

cannot access protected member declared in class from a friend function in derived class

i've a base class as follows:

//base class- arrayListType.h

class arrayListType
{
public:
//some function members;

protected:
elemType *list;
int length;
int maxSize;
};

and then a derived class:

//derived class- polynomialType.h

#include "arrayListType.h"

class polynomialType: public arrayListType<double>
{
friend ostream& operator<<(ostream& ,const polynomialType&);
friend istream& operator>>(istream& ,polynomialType&);

public:
polynomialType operator+(const polynomialType&);
polynomialType operator-(const polynomialType&);
//polynomialType operator*(const polynomialType&);
double operator() (double x);
polynomialType(int size = 100);
int min(int x,int y) const;
int max(int x,int y) const;
};

But after compiling the code, i got error as;

error C2248: 'arrayListType<elemType>::length' : cannot access protected member declared in class 'arrayListType<elemType>'

i've searched for solutions, but can't find ,please help. definition of operator>> is given for refference;

istream& operator>>(istream is,polynomialType& p)
{
cout << "the degree of this polynomial is" << p.length-1 << endl;

for (int i = 0; i < p.length; i++)
{
    cout << "enter coefficient of x^" << i <<": ";
    is >> p.list[i];
}
return is;
}

the error shows only for friend functions only, why is it so??

Upvotes: 0

Views: 1743

Answers (1)

inetknght
inetknght

Reputation: 4439

friend istream& operator>>(istream& ,polynomialType&);

verses

istream& operator>>(istream is,polynomialType& p)

Your stream function forgot the reference operator & and therefore has a different function signature. Not only that but it can also lead to subtle bugs such as copying the stream object (which may or may not have additional side effects) instead of re-using it.

Upvotes: 1

Related Questions