| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 818 人关注过本帖
标题:麻烦各位,问个问题
只看楼主 加入收藏
cavinlu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-9-24
收藏
 问题点数:0 回复次数:4 
麻烦各位,问个问题

在一个模板类内部说明另一个类是这个类的友元,

如下写法是否正确?

#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要加吗?

搜索更多相关主题的帖子: class arcType ArcNode Graph 麻烦 
2005-09-24 21:27
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include&lt;iostream&gt;
using namespace std;

int const MaxVertexes = 20;

template  &lt;class vertexType, class arcType&gt;
class ArcNode;  
template  &lt;class vertexType,class arcType&gt;
class VertexNode;

template  &lt;class vertexType, class arcType&gt;
class Graph
{
  // your code
};

template  &lt;class vertexType, class arcType&gt;  
class ArcNode
{  
  friend class Graph &lt;vertexType, arcType&gt;;
private:
  int adjvex;
  arcType weight;
  ArcNode&lt;vertexType, arcType&gt; * nextarc;
public:
  ArcNode(){}
  ArcNode(int v,arcType w):adjvex(v),weight(w),nextarc(NULL){}
};

template  &lt;class vertexType, class arcType&gt;  
class VertexNode
{
  friend class Graph &lt;vertexType, arcType&gt;;
private:
  vertexType data;
  ArcNode&lt;vertexType, arcType&gt; * firstarc;
public:
  VertexNode(){}
  VertexNode(vertexType v):data(v),firstarc(NULL){}
};

int main()
{
  // your code ...
  system("pause");
  return 0;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-09-24 23:40
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
老kai 用代码说话固然很好但是,别人问了问题总要说两个字的吧?
是让他们自己在代码里找答案?????
疑惑中。。。。。。。。。。

http://kongfuziandlife. http://codeanddesign.
2005-09-25 12:32
cavinlu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-9-24
收藏
得分:0 
版主,能否就我写的程序,编译器的错误给我说说原因?

                                                                                                   多谢了!
2005-09-25 15:17
cavinlu
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2005-9-24
收藏
得分:0 
版主,帮我看看这段程序,谢谢!
版主,能否帮我看看我的程序,我按你的做了,可编译还是通不过,晕啊!
kb6naqoE.rar (8.39 KB) 麻烦各位,问个问题

2005-09-25 16:54
快速回复:麻烦各位,问个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019278 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved