我是编程新手。我刚刚学习了c ++和一些数据结构的概念。 我正在尝试使用邻接列表编写图表表示。 我使用codeblocks作为我的编译器。但是,每当我尝试编译我的程序时,我都会收到以下错误...
C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 22 | error:变量或字段'initialize_graph'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 22 |错误:在','令牌之前预期的primary-expression | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 22 | error:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 23 | error:变量或字段'read_graph'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 23 |错误:在','标记之前预期的primary-expression | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 23 | error:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 | error:变量或字段'insert_edge'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |错误:在','令牌之前预期的primary-expression | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |错误:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |错误:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |错误:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 25 | error:变量或字段'print_graph'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 25 |错误:期望' - '之前的primary-expression'令牌| 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 | error:变量或字段'initialize_graph'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 36 |错误:'g'未在此范围内声明 C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 36 | error:在'int'|之前预期的primary-expression C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 46 | error:变量或字段'read_graph'声明为void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 46 |错误:'g'未在此范围内声明 C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 46 | error:在'int'|之前预期的primary-expression || ===构建完成: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 |错误:在'('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;
}
指出我做错了什么?
答案 0 :(得分:2)
你有一个结构和一个名为graph
的变量。不要那样做。
当您声明这些函数时,编译器会尝试使用变量...
答案 1 :(得分:2)
我认为代码
struct node
{
int y; /*adjacency info*/
int weight; /* edge weight, if any */
struct node *next; /* next edge in list */
} edgenode;
在typedef
之前缺少struct
。尝试将其添加到两个结构定义中。
没有typedef,它说:这是一个结构,顺便创建一个这种类型的变量。使用typedef,您实际上是在创建类型edgenode
和graph
。
答案 2 :(得分:1)
忘记声明函数的返回类型?例如,在您的最后一串代码中initialize_graph
应为
void initialize_graph(graph *g, int directed);
<强>更新强>
您对变量graph
的声明会使struct
同名。因此,当您声明函数以graph*
作为参数时,您指的不是类型而是变量。
另一个更新:
您声明指向graph
的指针并将其初始化为NULL
。然后尝试从函数中取消引用此类空指针。您必须使指针指向有效图形,例如通过指定它new graph()
;