我剛剛學習編程,剛剛學習了C++和一些數據結構的概念 我正在使用鄰接列表編碼圖表表達式 我正在使用但是,每當我嘗試編譯我的程序,我得到以下錯誤...期望的構造函數,析構函數或類型轉換之前'('token
C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 22 |錯誤:變量或字段'initialize_graph '聲明爲void | 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'聲明爲void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 23 |錯誤:在''之前預期的主表達式, C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 23 |錯誤:在'int'之前預期的primary-expression | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |錯誤:變量或字段'insert_edge'聲明爲void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 24 |錯誤:預期的主表達式','token | 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 |錯誤:變量或字段'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'聲明爲void | C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 46 |錯誤:'g'未在此範圍內聲明| C:\ Users \ Garg \ Desktop \ try \ Stack.cpp | 46 |錯誤:在'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";
}
}
而且,當我確定前主則功能我得到以下結果:
C:\用戶\加爾格\桌面\嘗試\ Stack.cpp ('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;
}
任何指向我在做什麼錯誤的指針?
我確實在我的代碼的第一個版本中添加了所有返回類型.. 但是仍然有些東西丟失了...... – user939442
我刪除了圖形變量。現在我只有圖形作爲結構。 該代碼現在符合,但投擲段錯誤 initialize_graph(graph * g,int directed) { int i; g - > nvertices = 0; //此行 g - > nedges = 0; g => directed = directed; (i = 1; i <= MAXV; i ++)g-> degree [i] = 0; (i = 1; i <= MAXV; i ++)g-> edges [i] = NULL; } – user939442
@ user939442:您的圖形指針初始化爲NULL,然後您嘗試解除引用。您需要初始化以指向第一個有效的東西。 –