【发布时间】:2015-02-27 16:09:41
【问题描述】:
我有一个基类如下:
//基类-arrayListType.h
class arrayListType
{
public:
//some function members;
protected:
elemType *list;
int length;
int maxSize;
};
然后是派生类:
//派生类-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;
};
但是编译代码后,我得到了错误:
error C2248: 'arrayListType<elemType>::length' : cannot access protected member declared in class 'arrayListType<elemType>'
我已经搜索了解决方案,但找不到,请帮助。 运算符>>的定义供参考;
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;
}
该错误仅显示朋友功能,为什么会这样??
【问题讨论】:
-
您是否尝试过将
friend函数放在您的public:定义下? -
friend声明不需要在任何特定的访问修饰符下。
标签: c++ inheritance friend