主题:哪位大哥帮我解决一下(关于图)
一个寻找最小树 的程序,怎么老是说没定义图呢!!
#ifndef _Graph_h_
#define _Graph_h_
enum ResultCode{NoMemory,OutOfBounds,Underflow,Overflow,Failure,NotPresent,Duplicate,Success};
template<class T>
class Graph
{
public:
virtual ResultCode Insert(int u,int v,T& w)=0;
virtual ResultCode Remove(int u,int v)=0;
virtual bool Exist(int u,int v)const=0;
virtual int Vertices()const{return n;};
protected:
int n,e;
};
#endif
#ifndef _LGraph_h_
#define _LGraph_h_
#include "Graph.h"
template<class T>
struct ENode
{
ENode(){ nextArc=NULL; }
ENode(int vertex,T weight,ENode* next)
{
adjVex=vertex;w=weight;nextArc=next;
}
int adjVex;
T w;
ENode* nextArc;
};
template<class T>
class LGraph:public Graph<T>
{
public:
LGraph(int mSize);
~LGraph();
ResultCode Insert(int u,int v,T& w);
ResultCode Remove(int u,int v);
bool Exist(int u,int v)const;
void Prim(int k);
void Prim(int k,int* nearest,T* lowest);
protected:
ENode<T> **a;
};
#endif
#include "LGraph.h"
/*#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=_FILE_;
#endif*/
template <class T>
LGraph<T>::LGraph(int mSize)
{
n=mSize;e=0;
a=new ENode<T>* [n];
for(int i=0;i<n;i++)a[i]=NULL;
}
//析构函数
template<class T>
LGraph<T>::~LGraph()
{
ENode<T> *p,*q;
for(int i=0;i<n;i++){
p=a[i];q=p;
while(p){
p=p->nextArc;delete q;q=p;
}
}
delete[] a;
}
//搜索函数
template<class T>
bool LGraph<T>::Exist(int u,int v)const
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return false;
ENode<T>* p=a[u];
while(p&&p->adjVex!=v) p=p->nextArc;
if(!p)return false;
else return true;
}
//插入函数
template<class T>
ResultCode LGraph<T>::Insert(int u,int v,T& w)
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return Failure;
if(Exist(u,v))return Duplicate;
ENode<T>* p=new ENode<T>(v,w,a[u]);
a[u]=p;e++;
return Success;
}
//删除函数
template<class T>
ResultCode LGraph<T>::Remove(int u,int v)
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return Failure;
ENode<T> *p=a[u],*q=NULL;
while(p&&p->adjVex!=v) { q=p;p=p->nextArc; }
if(!p)return NotPresent;
if(q)q->nextArc=p->nextArc;
else a[u]=p->nextArc;
delete p;
e--;
return Success;
}
template<class T>
void LGraph<T>::Prim(int k)
{
int* nearest=new int[n];
int* lowcost=new int[n];
Prim(0,nearest,lowcost);
for(int j=0;j<n;j++)
cout<<"("<<nearest[j]<<","<<j<<","<<lowcost[j]<<")";//输出到文件
cout<<endl;
}
template<class T>
void LGraph<T>::Prim(int k,int* nearest,T* lowcost)
{
bool* mark=new bool[n];
ENode<T> *p;
if(k<0||k>n-1) throw OutofBounds;
if(int i=0;i<n;i++)
{
nearest[i]=-1;mark[i]=false;
lowcost[i]=INFTY;
}
lowcost[k]=0;nearest[k]=k;mark[k]=true;
for(i=0;i<n;i++){
for(p=a[k];p;p=p->nextArc){
int j=p->adjVex;
if((!mark[j])&&(lowcost[j]>p->w))
{
lowcost[j]=p->w;nearest[j]=k;
}
}
T min=INFTY;
for(int j=0;j<n;j++)
if((!mark[j])&&(lowcost[j]<min)){
min=lowcost[j];k=j;
}
mark[k]=true;
}
}
#include <iostream.h>
#include "LGraph.cpp"
void main()
{
//直接建立结构体PipeData
//定义记录管段信息的结构
struct Pipe
{
int StartNum;
int EndNum;
double S;
};
//定义结构类型的数组
Pipe PipeData[10];
PipeData[0].StartNum=0;PipeData[0].EndNum=2;PipeData[0].S=1;
PipeData[1].StartNum=0;PipeData[1].EndNum=1;PipeData[1].S=6;
PipeData[2].StartNum=0;PipeData[2].EndNum=3;PipeData[2].S=5;
PipeData[3].StartNum=1;PipeData[3].EndNum=2;PipeData[3].S=5;
PipeData[4].StartNum=1;PipeData[4].EndNum=4;PipeData[4].S=3;
PipeData[5].StartNum=3;PipeData[5].EndNum=5;PipeData[5].S=2;
PipeData[6].StartNum=2;PipeData[6].EndNum=3;PipeData[6].S=5;
PipeData[7].StartNum=2;PipeData[7].EndNum=4;PipeData[7].S=6;
PipeData[8].StartNum=2;PipeData[8].EndNum=5;PipeData[8].S=4;
PipeData[9].StartNum=4;PipeData[9].EndNum=5;PipeData[9].S=6;
//生成图
CLGraph graph;
for(int i=0;i<10;i++)
{
graph.Insert(PipeData[i].StartNum,PipeData[i].EndNum,PipeData[i].S);
}
//生成最小树
graph.Prim(0);
}
编译后:
--------------------Configuration: tu - Win32 Debug--------------------
Compiling...
t.cpp
D:\tu\tu\t.cpp(33) : error C2065: 'CLGraph' : undeclared identifier
D:\tu\tu\t.cpp(33) : error C2146: syntax error : missing ';' before identifier 'graph'
D:\tu\tu\t.cpp(33) : error C2065: 'graph' : undeclared identifier
D:\tu\tu\t.cpp(36) : error C2228: left of '.Insert' must have class/struct/union type
D:\tu\tu\t.cpp(39) : error C2228: left of '.Prim' must have class/struct/union type
Error executing cl.exe.
tu.exe - 5 error(s), 0 warning(s)
这是怎么回事,小弟被折么死了.[em10]
#ifndef _Graph_h_
#define _Graph_h_
enum ResultCode{NoMemory,OutOfBounds,Underflow,Overflow,Failure,NotPresent,Duplicate,Success};
template<class T>
class Graph
{
public:
virtual ResultCode Insert(int u,int v,T& w)=0;
virtual ResultCode Remove(int u,int v)=0;
virtual bool Exist(int u,int v)const=0;
virtual int Vertices()const{return n;};
protected:
int n,e;
};
#endif
#ifndef _LGraph_h_
#define _LGraph_h_
#include "Graph.h"
template<class T>
struct ENode
{
ENode(){ nextArc=NULL; }
ENode(int vertex,T weight,ENode* next)
{
adjVex=vertex;w=weight;nextArc=next;
}
int adjVex;
T w;
ENode* nextArc;
};
template<class T>
class LGraph:public Graph<T>
{
public:
LGraph(int mSize);
~LGraph();
ResultCode Insert(int u,int v,T& w);
ResultCode Remove(int u,int v);
bool Exist(int u,int v)const;
void Prim(int k);
void Prim(int k,int* nearest,T* lowest);
protected:
ENode<T> **a;
};
#endif
#include "LGraph.h"
/*#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=_FILE_;
#endif*/
template <class T>
LGraph<T>::LGraph(int mSize)
{
n=mSize;e=0;
a=new ENode<T>* [n];
for(int i=0;i<n;i++)a[i]=NULL;
}
//析构函数
template<class T>
LGraph<T>::~LGraph()
{
ENode<T> *p,*q;
for(int i=0;i<n;i++){
p=a[i];q=p;
while(p){
p=p->nextArc;delete q;q=p;
}
}
delete[] a;
}
//搜索函数
template<class T>
bool LGraph<T>::Exist(int u,int v)const
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return false;
ENode<T>* p=a[u];
while(p&&p->adjVex!=v) p=p->nextArc;
if(!p)return false;
else return true;
}
//插入函数
template<class T>
ResultCode LGraph<T>::Insert(int u,int v,T& w)
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return Failure;
if(Exist(u,v))return Duplicate;
ENode<T>* p=new ENode<T>(v,w,a[u]);
a[u]=p;e++;
return Success;
}
//删除函数
template<class T>
ResultCode LGraph<T>::Remove(int u,int v)
{
if(u<0||v<0||u>n-1||v>n-1||u==v)return Failure;
ENode<T> *p=a[u],*q=NULL;
while(p&&p->adjVex!=v) { q=p;p=p->nextArc; }
if(!p)return NotPresent;
if(q)q->nextArc=p->nextArc;
else a[u]=p->nextArc;
delete p;
e--;
return Success;
}
template<class T>
void LGraph<T>::Prim(int k)
{
int* nearest=new int[n];
int* lowcost=new int[n];
Prim(0,nearest,lowcost);
for(int j=0;j<n;j++)
cout<<"("<<nearest[j]<<","<<j<<","<<lowcost[j]<<")";//输出到文件
cout<<endl;
}
template<class T>
void LGraph<T>::Prim(int k,int* nearest,T* lowcost)
{
bool* mark=new bool[n];
ENode<T> *p;
if(k<0||k>n-1) throw OutofBounds;
if(int i=0;i<n;i++)
{
nearest[i]=-1;mark[i]=false;
lowcost[i]=INFTY;
}
lowcost[k]=0;nearest[k]=k;mark[k]=true;
for(i=0;i<n;i++){
for(p=a[k];p;p=p->nextArc){
int j=p->adjVex;
if((!mark[j])&&(lowcost[j]>p->w))
{
lowcost[j]=p->w;nearest[j]=k;
}
}
T min=INFTY;
for(int j=0;j<n;j++)
if((!mark[j])&&(lowcost[j]<min)){
min=lowcost[j];k=j;
}
mark[k]=true;
}
}
#include <iostream.h>
#include "LGraph.cpp"
void main()
{
//直接建立结构体PipeData
//定义记录管段信息的结构
struct Pipe
{
int StartNum;
int EndNum;
double S;
};
//定义结构类型的数组
Pipe PipeData[10];
PipeData[0].StartNum=0;PipeData[0].EndNum=2;PipeData[0].S=1;
PipeData[1].StartNum=0;PipeData[1].EndNum=1;PipeData[1].S=6;
PipeData[2].StartNum=0;PipeData[2].EndNum=3;PipeData[2].S=5;
PipeData[3].StartNum=1;PipeData[3].EndNum=2;PipeData[3].S=5;
PipeData[4].StartNum=1;PipeData[4].EndNum=4;PipeData[4].S=3;
PipeData[5].StartNum=3;PipeData[5].EndNum=5;PipeData[5].S=2;
PipeData[6].StartNum=2;PipeData[6].EndNum=3;PipeData[6].S=5;
PipeData[7].StartNum=2;PipeData[7].EndNum=4;PipeData[7].S=6;
PipeData[8].StartNum=2;PipeData[8].EndNum=5;PipeData[8].S=4;
PipeData[9].StartNum=4;PipeData[9].EndNum=5;PipeData[9].S=6;
//生成图
CLGraph graph;
for(int i=0;i<10;i++)
{
graph.Insert(PipeData[i].StartNum,PipeData[i].EndNum,PipeData[i].S);
}
//生成最小树
graph.Prim(0);
}
编译后:
--------------------Configuration: tu - Win32 Debug--------------------
Compiling...
t.cpp
D:\tu\tu\t.cpp(33) : error C2065: 'CLGraph' : undeclared identifier
D:\tu\tu\t.cpp(33) : error C2146: syntax error : missing ';' before identifier 'graph'
D:\tu\tu\t.cpp(33) : error C2065: 'graph' : undeclared identifier
D:\tu\tu\t.cpp(36) : error C2228: left of '.Insert' must have class/struct/union type
D:\tu\tu\t.cpp(39) : error C2228: left of '.Prim' must have class/struct/union type
Error executing cl.exe.
tu.exe - 5 error(s), 0 warning(s)
这是怎么回事,小弟被折么死了.[em10]