| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 582 人关注过本帖
标题:[求助] 邻接表动态链 的 复制构造函数无法重载 ?
只看楼主 加入收藏
tianya1979
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-8-18
收藏
 问题点数:0 回复次数:1 
[求助] 邻接表动态链 的 复制构造函数无法重载 ?

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
const int M=8;// 图的顶点数

struct ArcNode
{
int adjvex;
ArcNode * next;
};

template <class T>
struct VertexNode
{
T vertex;
ArcNode * firstedge;
};

template <class T>
class ALGraph
{
public:
ALGraph(T a[]); //从键盘输入数据的构造函数
ALGraph(char * p);// 从磁盘读数据,构建有向图
ALGraph(ALGraph<T>&);//拷贝构造函数
~ALGraph()
{
cout<<"jieshu"<<endl;
}
void Print(int i); // 将顶点i及其父结点链 显示到显示器上
void Output();// 将图形的邻接表输出到文件
void M1(ArcNode *ap, int i, int n);//将i的父结点ap-〉adjvex与其它父结点连线,道义化
ALGraph<T> Moralize(ALGraph<T> a);//将对象道义化


private:
VertexNode <T> adjlist[M]; // 存放顶点表的数组
int vertexNum, arcNum; //图的顶点数和边数

};
template<class T> //从磁盘输入数据的构造函数
ALGraph<T>::ALGraph(char *p) // 指针p指向文件的路径及文件名
{
ifstream infile(p, ios::nocreate);
if(infile.fail())
{
cout<<"file no exist!"<<endl;
return;
}
int vertexNum, j, e=0;// e 用于统计边的数量
infile>>vertexNum; cout<<"vertexNum="<<vertexNum<<endl;
struct ArcNode * s, *s1;

for(int k=0; k<vertexNum; k++)
{
infile>>adjlist[k].vertex;
adjlist[k].firstedge=NULL;

infile>>j; // 输入边所依附的两个顶点的序号, j是k的父结点
int m=0;
while(j>k)
{
m++; e++; //每输入一条边,e的值就增加
s= new ArcNode;
s->adjvex=j;
if(m==1)
adjlist[k].firstedge=s;
else
s1->next=s;
s1=s;
infile>>j;
}
s1->next=NULL;

}
arcNum=e; cout<<"arcNum="<<arcNum<<endl;
infile.close;
}

template<class T>
ALGraph<T>::ALGraph(const ALGraph<T>&g) //拷贝构造函数
{
vertexNum=g.vertexNum; arcNum=g.arcNum;
for(int i=0; i< vertexNum; i++)
{
adjlist[i].vertex=g.adjlist[i].vertex;
ArcNode *bs, *bs1, *as;
as=g.adjlist[i].firstedge;
if(as!=NULL)
{
bs=new ArcNode;
bs->adjvex=as->adjvex;
adjlist[i].firstedge=bs;
bs1=bs;
as=as->next;
while(as!=NULL)
{
bs=new ArcNode;
bs->adjvex=as->adjvex;
bs1->next=bs;
bs1=bs;
as=as->next;

}
bs->next=NULL;

}
else
{
adjlist[i].firstedge=NULL;
}
}

}template <class T>
void ALGraph<T>::Print(int i)// 将顶点i及其父结点显示到显示器上
{
struct ArcNode * p;
cout<<i<<" ";
p= adjlist[i].firstedge;
while(p!=NULL)
{
cout<<p->adjvex<<" ";
p=p->next;
}
cout<<endl;
}
void main()
{ ALGraph<int> g2("d:\\grahp2.txt"); // 从d:\\grahp2.txt读入数据,来构造g2

for(int i=0; i<M; i++)
{
g2.Print(i);

}

}
拷贝构造函数无法重载? 为什么,怎么改? 谢谢

搜索更多相关主题的帖子: 函数 构造 动态 重载 
2006-08-27 10:43
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
声明和定义的时候,参数不一致,const

[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-08-27 12:18
快速回复:[求助] 邻接表动态链 的 复制构造函数无法重载 ?
数据加载中...
 
   



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

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