0

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??

bikrathor
  • 96
  • 1
  • 1
  • 7

1 Answers1

1
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.

inetknght
  • 4,300
  • 1
  • 26
  • 52
  • Streams are not copyable; the only thing that could happen here would be moving a stream in (which is hard not to notice). – Griwes Jan 02 '15 at 09:10
  • Indeed, STL streams are not copyable; non-standard streams may be copyable. Without `::std::` fully-qualified identifier, namespace issues may cause `istream` to resolve to a custom type of the same name. – inetknght Jan 02 '15 at 15:15