【问题标题】:expected constructor, destructor, or type conversion before '(' token'(' 标记之前的预期构造函数、析构函数或类型转换
【发布时间】:2011-12-04 21:35:51
【问题描述】:

我是编程新手。我刚刚学习了 c++ 和一些数据结构的概念。 我正在尝试使用邻接表编写图形表示。 我使用代码块作为我的编译器。但是,每当我尝试编译我的程序时,我都会收到以下错误...

C:\Users\Garg\Desktop\try\Stack.cpp|22|错误:变量或字段“initialize_graph”声明为无效| C:\Users\Garg\Desktop\try\Stack.cpp|22|error: ',' 标记之前的预期主表达式| 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|error: ',' token| 之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|23|错误:“int”之前的预期主表达式| C:\Users\Garg\Desktop\try\Stack.cpp|24|错误:变量或字段 'insert_edge' 声明为 void| 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' 声明为 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”声明为无效| 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;
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|error: '(' 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;
}

有什么提示我做错了什么?

【问题讨论】:

    标签: c++


    【解决方案1】:

    你有一个名为graph 的结构体和一个变量。不要那样做。

    当您声明这些函数时,编译器会尝试使用该变量...

    【讨论】:

    • 不,没关系,变量会影响结构。
    • 当然,如果它隐藏了结构,那么函数声明都是指变量!
    • @Chris A.:或者只是 struct graph { ... };,这是 C++ 而不是 C。
    【解决方案2】:

    我认为代码

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

    struct 之前缺少typedef。尝试将它添加到两个结构定义中。

    没有typedef,它说:这是一个结构,顺便创建一个这种类型的变量。使用 typedef,您实际上是在创建类型 edgenodegraph

    【讨论】:

    • 或者,OP应该改写为struct node {...}; node edgenode;
    • @ThomasMatthews 据我所知,edgenode 被用作print_graph 中的一个类型,所以这可能不会奏效。
    【解决方案3】:

    忘记声明函数的返回类型?例如,在你的最后一堆代码中initialize_graph 应该是

    void initialize_graph(graph *g, int directed);
    

    更新:

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

    另一个更新:

    您声明一个指向graph 的指针并将其初始化为NULL。然后,您尝试从您的函数中取消引用此类空指针。您必须使指针指向一个有效的图形,例如通过分配给它new graph();

    【讨论】:

    • 我确实在我的代码的第一个版本中添加了所有返回类型。但仍然缺少一些东西......
    • 我删除了图形变量。现在我只有图形作为结构。代码现在符合但在 initialize_graph(graph *g, intdirected) { int i; 处引发分段错误g -> nvertices = 0; // 这一行 g -> 边缘 = 0; g -> 定向 = 定向;对于 (i=1; idegree[i] = 0;对于 (i=1; iedges[i] = NULL; }
    • @user939442:您的图形指针已初始化为 NULL,然后您尝试取消引用。您需要先初始化以指向有效的东西。
    猜你喜欢
    • 2010-12-12
    • 2011-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多