“二进制表达式的无效操作数”错误
“Invalid operands to binary expressions” Error
我不断收到此错误:
Invalid operands to binary expressions ('struct node'and 'int')
我在下面用 ** 标记的两行函数 "reachR"。出了什么问题,我该如何解决?该代码用于数据结构分配。
#include <stdio.h>
#include <stdlib.h>
#define Vertex int
#define maxV 10000
typedef struct digraph *Digraph;
typedef struct node *teste;
static int lbl[maxV];
typedef struct {
Vertex v, w;
} Arc;
struct digraph {
int V;
int A;
teste *adj;
};
struct node {
Vertex w;
teste next;
};
teste NEWnode( Vertex w, teste next) {
teste a = malloc( sizeof (struct node));
a->w = w;
a->next = next;
return a;
}
Arc ARC( Vertex v, Vertex w) {
Arc a;
a.v = v, a.w = w;
return a;
}
void reachR( Digraph G, Vertex v) {
Vertex w;
lbl[v] = 1;
for (w = 0; w < G->V; w++)
**if (G->adj[v][w] == 1 && lbl[w] == 0)**
reachR( G, w);
}
int DIGRAPHreach( Digraph G, Vertex s, Vertex t) {
Vertex v;
for (v = 0; v < G->V; v++)
lbl[v] = 0;
reachR( G, s);
if (lbl[t] == 0) return 0;
else return 1;
}
int digraphcycle( Digraph G) {
Vertex v; teste a; int output;
for (v = 0; v < G->V; v++)
for (a = G->adj[v]; a != NULL; a = a->next) {
output = DIGRAPHreach( G, a->w, v);
if (output == 1) return 1;
}
return 0;
}
Digraph DIGRAPHinit( int V) {
Vertex v;
Digraph G = malloc( sizeof *G);
G->V = V;
G->A = 0;
G->adj = malloc( V * sizeof (teste));
for (v = 0; v < V; v++)
G->adj[v] = NULL;
return G;
}
void DIGRAPHinsertA( Digraph G, Vertex v, Vertex w) {
teste a;
for (a = G->adj[v]; a != NULL; a = a->next)
if (a->w == w) return;
G->adj[v] = NEWnode( w, G->adj[v]);
G->A++;
}
int main (){
Digraph G = DIGRAPHinit(4);
DIGRAPHinsertA(G, 1, 2);
DIGRAPHinsertA(G, 1, 3);
DIGRAPHinsertA(G, 2, 3);
DIGRAPHinsertA(G, 3, 4);
if (digraphcycle(G)==1){
printf("SIM!");
}
}
您正在尝试比较 struct
和 int
。显然是做不到的。等于运算符 (==
) 是一个二元运算符,它需要两边都有两个可比较的对象。
您可以通过执行
访问评论中提到的Vertex
(int
)的值
G->adj[v]->w
我不断收到此错误:
Invalid operands to binary expressions ('struct node'and 'int')
我在下面用 ** 标记的两行函数 "reachR"。出了什么问题,我该如何解决?该代码用于数据结构分配。
#include <stdio.h>
#include <stdlib.h>
#define Vertex int
#define maxV 10000
typedef struct digraph *Digraph;
typedef struct node *teste;
static int lbl[maxV];
typedef struct {
Vertex v, w;
} Arc;
struct digraph {
int V;
int A;
teste *adj;
};
struct node {
Vertex w;
teste next;
};
teste NEWnode( Vertex w, teste next) {
teste a = malloc( sizeof (struct node));
a->w = w;
a->next = next;
return a;
}
Arc ARC( Vertex v, Vertex w) {
Arc a;
a.v = v, a.w = w;
return a;
}
void reachR( Digraph G, Vertex v) {
Vertex w;
lbl[v] = 1;
for (w = 0; w < G->V; w++)
**if (G->adj[v][w] == 1 && lbl[w] == 0)**
reachR( G, w);
}
int DIGRAPHreach( Digraph G, Vertex s, Vertex t) {
Vertex v;
for (v = 0; v < G->V; v++)
lbl[v] = 0;
reachR( G, s);
if (lbl[t] == 0) return 0;
else return 1;
}
int digraphcycle( Digraph G) {
Vertex v; teste a; int output;
for (v = 0; v < G->V; v++)
for (a = G->adj[v]; a != NULL; a = a->next) {
output = DIGRAPHreach( G, a->w, v);
if (output == 1) return 1;
}
return 0;
}
Digraph DIGRAPHinit( int V) {
Vertex v;
Digraph G = malloc( sizeof *G);
G->V = V;
G->A = 0;
G->adj = malloc( V * sizeof (teste));
for (v = 0; v < V; v++)
G->adj[v] = NULL;
return G;
}
void DIGRAPHinsertA( Digraph G, Vertex v, Vertex w) {
teste a;
for (a = G->adj[v]; a != NULL; a = a->next)
if (a->w == w) return;
G->adj[v] = NEWnode( w, G->adj[v]);
G->A++;
}
int main (){
Digraph G = DIGRAPHinit(4);
DIGRAPHinsertA(G, 1, 2);
DIGRAPHinsertA(G, 1, 3);
DIGRAPHinsertA(G, 2, 3);
DIGRAPHinsertA(G, 3, 4);
if (digraphcycle(G)==1){
printf("SIM!");
}
}
您正在尝试比较 struct
和 int
。显然是做不到的。等于运算符 (==
) 是一个二元运算符,它需要两边都有两个可比较的对象。
您可以通过执行
访问评论中提到的Vertex
(int
)的值
G->adj[v]->w