对于一个父类,我声明了一些私有成员变量,并声明它的子类为父类的友元。在子类的方法中我用到了父类的变量,但编译器却告诉我没有声明。附上代码。
#include <iostream>
#include "Chain.h"
class LinkedDigraph;
template<class T>
class LinkedWDigraph;
template<class T>
class LinkedBase{
friend LinkedDigraph;
friend LinkedWDigraph<T>;
public:
LinkedBase(int Vertices=10){
n=Vertices;
e=0;
h=new Chain<T>[n+1];
}
~LinkedBase(){
delete [] h;
}
int Edges() const{
return e;
}
int Vertices() const{
return n;
}
int OutDegree(int i) const{
if(i<1||i>n) throw "wrong info";
return h[i].length;
}
void InitializePos(){
pos=new ChainIterator<T> [n+1];
}
void DeactivatePos(){
delete []pos;
}
private:
int n,e;
Chain<T> *h;
ChainIterator<T>* pos;
};
template<class T>
class LinkedWGraph;
template<class T>
class GraphNode{
friend LinkedWDigraph<T>;
friend LinkedWGraph<T>;
friend Chain<T>;
private:
int vertex;
T weight;
};
template <class T>
class LinkedWDigraph:public LinkedBase<GraphNode<T> >{//Note:两个右尖括号不能连续使用
public:
LinkedWDigraph(int Vertices=10): LinkedBase<GraphNode<T> >(Vertices){}
LinkedWDigraph<T>& Add(int i,int j,const T& w);
protected:
LinkedWDigraph<T>& AddNoCheck(int i,int j,const T&w);
};
template<class T>
LinkedWDigraph<T>& LinkedWDigraph<T>::Add(int i,int j,const T& w){
return AddNoCheck(i,j,w);
}
template<class T>
LinkedWDigraph<T>& LinkedWDigraph<T>::AddNoCheck(int i,int j,const T& w){
GraphNode<T> x;
x.vertex=j;
x.weight=w;
h[i].Insert(0,x);
e++;
return *this;
}
错误提示:69 2 C:\Users\dell\Documents\LinkedBase.cpp [Error] 'h' was not declared in this scope
70 2 C:\Users\dell\Documents\LinkedBase.cpp [Error] 'e' was not declared in this scope