在一个模板类内部说明另一个类是这个类的友元,
如下写法是否正确?
#include<iostream.h> #define MaxVertexes 20 template <class vertexType,class arcType> class Graph;
template <class arcType> class ArcNode{ friend class Graph<class vertexType,class arcType>; private: int adjvex; arcType weight; ArcNode<arcType>*nextarc; public: ArcNode(){} ArcNode(int v,arcType w):adjvex(v),weight(w),nextarc(NULL){} }; template <class vertexType,class arcType> class VertexNode{ friend class Graph <class vertexType,class arcType>; private: vertexType data; ArcNode<arcType>*firstarc; public: VertexNode(){} VertexNode(vertexType v):data(v),firstarc(NULL){} };
我想说明ArcNode和VertexNode为Graph的友元,
friend class Graph<class vertexType,class arcType>;
这句话的下划线的class要不要加,以前都是不加的,编译能通过的,这次老是说有个错误!
如果不加class,两处加下划线和加粗的地方都去掉的话,编译器报如下错误:
Compiling... Graph.cpp F:\tmp\Graph\Graph.cpp(5) : error C2065: 'vertexType' : undeclared identifier F:\tmp\Graph\Graph.cpp(13) : see reference to class template instantiation 'ArcNode<arcType>' being compiled Error executing cl.exe.
Graph.obj - 1 error(s), 0 warning(s)
请问这种格式对吗?class要加吗?