2011-10-10 50 views
1

我剛剛學習編程,剛剛學習了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; 
} 

任何指向我在做什麼錯誤的指針?

回答

0

忘記爲你的函數聲明返回類型?例如,在代碼的最後一堆initialize_graph應該

void initialize_graph(graph *g, int directed); 

更新:

你一個變量陰影struct具有相同名稱的聲明。因此,當你以graph*作爲參數聲明你的函數時,你所指的不是一個類型,而是一個變量。

另一個更新:

你聲明的指針並初始化爲NULL。然後嘗試從函數中取消引用這樣的空指針。你必須使指針指向一個有效的圖,例如通過分配給它new graph();

+0

我確實在我的代碼的第一個版本中添加了所有返回類型.. 但是仍然有些東西丟失了...... – user939442

+0

我刪除了圖形變量。現在我只有圖形作爲結構。 該代碼現在符合,但投擲段錯誤 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

+0

@ user939442:您的圖形指針初始化爲NULL,然後您嘗試解除引用。您需要初始化以指向第一個有效的東西。 –

1

你有一個結構和一個變量,稱爲。不要這樣做。

在聲明編譯器嘗試使用變量的函數...

+0

不,沒關係,變量會影響結構。 –

+0

當然,如果它影響結構,那麼函數聲明都指的是變量! –

+0

好吧,我刪除圖形作爲一個結構,它幫助... – user939442

1

我覺得代碼

struct node 
{ 
    int y;    /*adjacency info*/ 
    int weight;    /* edge weight, if any */ 
    struct node *next;  /* next edge in list */ 
} edgenode; 

struct之前缺少typedef。嘗試將其添加到兩個結構定義。

沒有typedef,它說:這是一個結構,並順便創建一個這種類型的變量。使用typedef,您實際上正在創建類型edgenode和。

+0

或者,OP應該改爲'struct節點{...};節點edgenode;' –

+0

@ThomasMatthews從我看到的'edgenode'被用作'print_graph'中的一個類型,所以這可能不會成功。 – vhallac

相關問題