在'之前应进行构造函数、析构函数或类型转换;(';代币 我对编程很陌生。我刚刚学习了C++的概念和一些数据结构。 我正在尝试使用邻接列表编写一个图形表示。 我使用代码块作为我的编译器。然而,每当我试图编译我的程序时,我会得到以下错误

在'之前应进行构造函数、析构函数或类型转换;(';代币 我对编程很陌生。我刚刚学习了C++的概念和一些数据结构。 我正在尝试使用邻接列表编写一个图形表示。 我使用代码块作为我的编译器。然而,每当我试图编译我的程序时,我会得到以下错误,c++,C++,C:\Users\Garg\Desktop\try\Stack.cpp | 22 |错误:变量或字段“initialize_graph”声明无效| C:\Users\Garg\Desktop\try\Stack.cpp | 22 |错误:应在“,”标记之前使用主表达式| C:\Users\Garg\Desktop\try\Stack.cpp | 22 |错误:应在“int”之前使用主表达式| C:\Users\Garg\Desktop\try\Stack.cpp | 23 |错误:变量或字段“r

C:\Users\Garg\Desktop\try\Stack.cpp | 22 |错误:变量或字段“initialize_graph”声明无效| C:\Users\Garg\Desktop\try\Stack.cpp | 22 |错误:应在“,”标记之前使用主表达式| 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 |错误:应在“,”标记之前使用主表达式| C:\Users\Garg\Desktop\try\Stack.cpp | 23 |错误:应在“int”之前使用主表达式| C:\Users\Garg\Desktop\try\Stack.cpp | 24 |错误:变量或字段“insert_edge”声明无效| 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”声明无效| C:\Users\Garg\Desktop\try\Stack.cpp | 25 |错误:在“')标记之前应该有主表达式| 函数“int main()”中的C:\Users\Garg\Desktop\try\Stack.cpp | |:| C:\Users\Garg\Desktop\try\Stack.cpp | 32 |错误:未在此范围内声明“read_graph”| C:\Users\Garg\Desktop\try\Stack.cpp | 33 |错误:“print|u graph”未在此范围内声明| C:\Users\Garg\Desktop\try\Stack.cpp | 36 |错误:变量或字段“initialize_graph”声明无效| 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;


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;
}
这是我的节目:

#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";
}
}
#包括
#定义MAXV 1000/*最大顶点数*/
使用名称空间std;
结构节点
{ 
int y;/*邻接信息*/
整数权重;/*边缘权重(如有)*/
结构节点*next;/*列表中的下一条边*/
}边节点;
结构图{
节点*边[MAXV+1];/*邻接信息*/
int度[MAXV+1];/*每个顶点的outdegree*/
int nvertices;/*图中的顶点数*/
int nedges;/*图中的边数*/
int directed;/*图形是否有方向*/
}图形;
无效初始化图(图*,int);
无效读取图(图*,int);
无效插入_边(图*,int,int,int);
无效打印图(图*);
int main()
{
结构图*g=NULL;
cout-nvertices=0;
g->nedges=0;
g->定向=定向;
对于(i=1;idegree[i]=0;
对于(i=1;iedges[i]=NULL;
}
void read_图(图*g,int-directed)
{
int i;
int m;
int x,y;/*边(x,y)中的顶点*/
初始化图(g,有向);
cout>g->n证书;
cin>>m;
cout x;
cin>>y;
插入_边(g、x、y、定向);
}
}
无效插入_边(图*g,整数x,整数y,整数定向)
{
结构节点*p;
p=malloc(sizeof(struct node));
p->weight=NULL;
p->y=y;
p->next=g->边[x];
g->边[x]=p;/*在列表的开头插入*/
g->度[x]++;
如果(定向==0)
插入_边(g,y,x,1);
其他的
g->nedges++;
}
无效打印图(图*g)
{
int i;/*计数器*/
edgenode*p;/*临时指针*/
对于(i=1;反转;i++){
cout-edges[i];
while(p!=NULL){
库蒂;
p=p->next;
}
cout-nvertices=0;
g->nedges=0;
g->定向=定向;
对于(i=1;idegree[i]=0;
对于(i=1;iedges[i]=NULL;
}
读图(图*g,整数定向)
{
int i;
int m;
int x,y;/*边(x,y)中的顶点*/
初始化图(g,有向);
cout>g->n证书;
cin>>m;
cout x;
cin>>y;
插入_边(g、x、y、定向);
}
}
插入_边(图*g,整数x,整数y,整数定向)
{
结构节点*p;
p=malloc(sizeof(struct node));
p->weight=NULL;
p->y=y;
p->next=g->边[x];
g->边[x]=p;/*在列表的开头插入*/
g->度[x]++;
如果(定向==0)
插入_边(g,y,x,1);
其他的
g->nedges++;
}
打印图形(图形*g)
{
int i;/*计数器*/
edgenode*p;/*临时指针*/
对于(i=1;反转;i++){
cout-edges[i];
while(p!=NULL){
库蒂;
p=p->next;
}

难道忘了声明函数的返回类型吗?例如,在上一组代码中,
initialize\u graph
应该是

void initialize_graph(graph *g, int directed);
更新:

您对变量
graph
的声明会使同名的
struct
隐藏起来。因此,当您以
graph*
作为参数声明函数时,您指的不是类型,而是变量

另一个更新:


声明指向
图形的指针
,并将其初始化为
NULL
。然后尝试从函数中取消引用此类空指针。必须使指针指向有效的图形,例如,为其指定
新图形()

忘记声明函数的返回类型了吗?例如,在上一组代码中,
initialize\u graph
应该是

void initialize_graph(graph *g, int directed);
更新:

您对变量
graph
的声明会使同名的
struct
隐藏起来。因此,当您以
graph*
作为参数声明函数时,您指的不是类型,而是变量

另一个更新:

声明指向
图形的指针
,并将其初始化为
NULL
。然后重试