Reputation: 7
I am new to programming. I just learned concepts of c++ and some data structures. I am trying to code a graph representation using adjacency list. I am using codeblocks as my compiler. However, whenever I try to compile my program i get the following error...
C:\Users\Garg\Desktop\try\Stack.cpp|22|error: variable or field 'initialize_graph' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|22|error: expected primary-expression before ',' token| C:\Users\Garg\Desktop\try\Stack.cpp|22|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|23|error: variable or field 'read_graph' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|23|error: expected primary-expression before ',' token| C:\Users\Garg\Desktop\try\Stack.cpp|23|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|24|error: variable or field 'insert_edge' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|24|error: expected primary-expression before ',' token| C:\Users\Garg\Desktop\try\Stack.cpp|24|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|24|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|24|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|25|error: variable or field 'print_graph' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|25|error: expected primary-expression before ')' token| C:\Users\Garg\Desktop\try\Stack.cpp||In function 'int main()':| C:\Users\Garg\Desktop\try\Stack.cpp|32|error: 'read_graph' was not declared in this scope| C:\Users\Garg\Desktop\try\Stack.cpp|33|error: 'print_graph' was not declared in this scope| C:\Users\Garg\Desktop\try\Stack.cpp|36|error: variable or field 'initialize_graph' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|36|error: 'g' was not declared in this scope| C:\Users\Garg\Desktop\try\Stack.cpp|36|error: expected primary-expression before 'int'| C:\Users\Garg\Desktop\try\Stack.cpp|46|error: variable or field 'read_graph' declared void| C:\Users\Garg\Desktop\try\Stack.cpp|46|error: 'g' was not declared in this scope| C:\Users\Garg\Desktop\try\Stack.cpp|46|error: expected primary-expression before 'int'| ||=== Build finished: 21 errors, 0 warnings ===|
Here is my program:
#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";
}
}
Moreover, when i define the functions before main then i get the following result:
C:\Users\Garg\Desktop\try\Stack.cpp|23|error: expected constructor, destructor, or type conversion before '(' token| C:\Users\Garg\Desktop\try\Stack.cpp|33|error: expected constructor, destructor, or type conversion before '(' token| ||=== Build finished: 2 errors, 0 warnings ===|
#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;
}
Any pointers to what wrong am I doing?
Upvotes: 1
Views: 3776
Reputation: 81349
Forgetting to declare the return type for your functions? For instance, in your last bunch of code initialize_graph
should be
void initialize_graph(graph *g, int directed);
Update:
Your declaration of a variable graph
shadows the struct
with the same name. Hence, when you declare your functions taking graph*
as an argument you are referring not to a type but to a variable.
Another Update:
You declare a pointer to graph
and initialize it to NULL
. You then try to dereference such null pointer from within your functions. You have to make the pointer point to a valid graph, for instance by assigning to it new graph()
;
Upvotes: 0
Reputation: 13907
I think the code
struct node
{
int y; /*adjacency info*/
int weight; /* edge weight, if any */
struct node *next; /* next edge in list */
} edgenode;
is missing a typedef
before the struct
. Try adding it to both struct definitions.
Without the typedef, it says: here is a structure, and by the way create a variable of this type. With the typedef, you are actually creating the types edgenode
and graph
.
Upvotes: 1
Reputation: 96266
you have both a struct and a variable called graph
. Don't do that.
When you declare those functions the compiler tries to use the variable...
Upvotes: 1