【发布时间】:2011-12-04 21:35:51
【问题描述】:
我是编程新手。我刚刚学习了 c++ 和一些数据结构的概念。 我正在尝试使用邻接表编写图形表示。 我使用代码块作为我的编译器。但是,每当我尝试编译我的程序时,我都会收到以下错误...
C:\Users\Garg\Desktop\try\Stack.cpp|22|错误:变量或字段“initialize_graph”声明为无效| C:\Users\Garg\Desktop\try\Stack.cpp|22|error: ',' 标记之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|22|错误:“int”之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|23|错误:变量或字段“read_graph”声明为无效| C:\Users\Garg\Desktop\try\Stack.cpp|23|error: ',' token| 之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|23|错误:“int”之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:变量或字段 'insert_edge' 声明为 void| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:',' 标记之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:'int'之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:'int'之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:'int'之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|25|错误:变量或字段 'print_graph' 声明为 void| C:\Users\Garg\Desktop\try\Stack.cpp|25|错误:')' 标记之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp||在函数'int main()':| C:\Users\Garg\Desktop\try\Stack.cpp|32|错误:'read_graph' 未在此范围内声明| C:\Users\Garg\Desktop\try\Stack.cpp|33|错误:'print_graph' 未在此范围内声明| C:\Users\Garg\Desktop\try\Stack.cpp|36|错误:变量或字段 'initialize_graph' 声明为 void| C:\Users\Garg\Desktop\try\Stack.cpp|36|错误:'g' 未在此范围内声明| C:\Users\Garg\Desktop\try\Stack.cpp|36|错误:'int'之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|46|错误:变量或字段“read_graph”声明为无效| C:\Users\Garg\Desktop\try\Stack.cpp|46|错误:'g' 未在此范围内声明| C:\Users\Garg\Desktop\try\Stack.cpp|46|错误:'int'之前的预期主表达式| ||=== 构建完成:21 个错误,0 个警告 ===|
这是我的程序:
#include<iostream>
#define MAXV 1000 /* maximum number of vertices */
using namespace std;
struct node
{
int y; /*adjacency info*/
int weight; /* edge weight, if any */
struct node *next; /* next edge in list */
} edgenode;
struct graph{
node *edges[MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in graph */
int nedges; /* number of edges in graph */
int directed; /* is the graph directed? */
} graph;
void initialize_graph (graph *, int);
void read_graph (graph *, int);
void insert_edge (graph *, int, int, int);
void print_graph (graph *);
int main()
{
struct graph *g = NULL;
cout << "Now reading graph";
read_graph(g, 1);
print_graph(g);
return 0;
}
void initialize_graph(graph *g, int directed)
{
int i;
g -> nvertices = 0;
g -> nedges = 0;
g -> directed = directed;
for (i=1; i<=MAXV; i++) g->degree[i] = 0;
for (i=1; i<=MAXV; i++) g->edges[i] = NULL;
}
void read_graph(graph *g, int directed)
{
int i;
int m;
int x, y; /* vertices in edge (x,y) */
initialize_graph(g, directed);
cout << "Enter the number of vertivces and edges";
cin >> g->nvertices;
cin >> m;
cout << "Enter the vertices for the edge and the weight of the edge";
for (i=1; i<=m; i++) {
cin >> x;
cin >> y;
insert_edge(g,x,y,directed);
}
}
void insert_edge(graph *g, int x, int y, int directed)
{
struct node *p;
p = malloc(sizeof(struct node));
p->weight = NULL;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p; /* insert at head of list */
g->degree[x] ++;
if (directed == 0)
insert_edge(g,y,x,1);
else
g->nedges ++;
}
void print_graph(graph *g)
{
int i; /* counter */
edgenode *p; /* temporary pointer */
for (i=1; i<=g->nvertices; i++) {
cout << i;
p = g->edges[i];
while (p != NULL) {
cout << p->y;
p = p->next;
}
cout << "\n";
}
}
此外,当我在 main 之前定义函数时,我得到以下结果:
C:\Users\Garg\Desktop\try\Stack.cpp|23|error: '(' token| 之前的预期构造函数、析构函数或类型转换 C:\Users\Garg\Desktop\try\Stack.cpp|33|error: '(' token| 之前的预期构造函数、析构函数或类型转换 ||=== 构建完成:2 个错误,0 个警告 ===|
#include<iostream>
#define MAXV 1000 /* maximum number of vertices */
using namespace std;
struct node
{
int y; /*adjacency info*/
int weight; /* edge weight, if any */
struct node *next; /* next edge in list */
} edgenode;
struct graph{
node *edges[MAXV+1]; /* adjacency info */
int degree[MAXV+1]; /* outdegree of each vertex */
int nvertices; /* number of vertices in graph */
int nedges; /* number of edges in graph */
int directed; /* is the graph directed? */
} graph;
initialize_graph(graph *g, int directed)
{
int i;
g -> nvertices = 0;
g -> nedges = 0;
g -> directed = directed;
for (i=1; i<=MAXV; i++) g->degree[i] = 0;
for (i=1; i<=MAXV; i++) g->edges[i] = NULL;
}
read_graph(graph *g, int directed)
{
int i;
int m;
int x, y; /* vertices in edge (x,y) */
initialize_graph(g, directed);
cout << "Enter the number of vertivces and edges";
cin >> g->nvertices;
cin >> m;
cout << "Enter the vertices for the edge and the weight of the edge";
for (i=1; i<=m; i++) {
cin >> x;
cin >> y;
insert_edge(g,x,y,directed);
}
}
insert_edge(graph *g, int x, int y, int directed)
{
struct node *p;
p = malloc(sizeof(struct node));
p->weight = NULL;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p; /* insert at head of list */
g->degree[x] ++;
if (directed == 0)
insert_edge(g,y,x,1);
else
g->nedges ++;
}
print_graph(graph *g)
{
int i; /* counter */
edgenode *p; /* temporary pointer */
for (i=1; i<=g->nvertices; i++) {
cout << i;
p = g->edges[i];
while (p != NULL) {
cout << p->y;
p = p->next;
}
cout << "\n";
}
}
int main()
{
struct graph *g = NULL;
cout << "Now reading graph";
read_graph(g, 1);
print_graph(g);
return 0;
}
有什么提示我做错了什么?
【问题讨论】:
标签: c++