Re: A scope problem??



On Apr 15, 7:08 pm, Keith Thompson <ks...@xxxxxxx> wrote:
Andy <wherea...@xxxxxxxxx> writes:
On Apr 15, 5:59 pm, Nate Eldredge <n...@xxxxxxxxxx> wrote:
[...]
The type `SGL' is probably not getting defined.  Make sure the header
with the appropriate typedef is being included.

Thanks!.
-----------------------------------------------------<heap.h>
  1 #ifndef HEAP_H_
  2 #define HEAP_H_
  3
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <assert.h>
  7
  8 #include "type.h"
  9 #include "lib.h"
 10
 11 #define DELETED -1
 12
 13 typedef struct node{
 14     int vid;
 15     int degree;
 16 }NODE;

...
#endif
-----------------------------------------------------<heap.h>

-----------------------------------------------------<loadgraph.h>
  1 #ifndef LOAD_GRAPH_H_
  2 #define LOAD_GRAPH_H_
  3
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <string.h>
  7 #include <assert.h>
  8
  9 #include "hashtbl.h"
 10 #include "shingle.h"
 11 #include "heap.h"
 12 #include "loadgraph.h"
 13
 14 /* syntax: #define name (var name) arbitrary text */
 15 #define HEAD_DEL ":"
 16 #define LIST_DEL ";"
 17 #define MAX_GID_LEN 40
 18
 19
 20 #define PRUNED -1
 21 #define ADJ_END -2
 22
 23 extern char **n2gidHash;
 24 extern SGL *gFSgl;
 25 extern int gN;
 26 extern int gC;
 27 extern int gS;

...
#endif

-----------------------------------------------------<loadgraph.h>

So, SGL is defined....

So you say, but you haven't demonstrated it.  The first occurrence of
"SGL" in the above code is in the declaration "extern SGL *gFSgl;".

Perhaps you're assuming that SGL is defined in one of the headers you
haven't shown us -- but if it were, you probably wouldn't be getting
the error message you reported upthread.

Could it be that SGL is defined as a struct rather than as a typedef?
For example, if you have:

    struct SGL {
        /* ... */
    };

then the name SGL isn't recognized unless it follows the "struct"
keyword.  If that's the case, then you could declare:

    extern struct SGL *gFSgl;

A side point: You should add parenthese to some of your macro
definitions.  In heap.h:

    #define DELETED (-1)

In loadgraph.h:

    #define PRUNED (-1)
    #define ADJ_END (-2)

Without the parentheses, DELETED, for example, expands to a sequence
of two tokens, ``-'' and ``1''; depending on the context, that might
not necessarily be a unary minus applied to the constant 1.

Also, line numbers are generally a bad idea.  They make it difficult
for us to grab a copy of your code and try it ourselves.  You can add
a comment on a specific line, such as:

    extern SGL *gFSgl; /* line 24 */

Then again you haven't shown us enough code so that we'd be able to
compile it anyway.

--
Keith Thompson (The_Other_Keith) ks...@xxxxxxx  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks guys.

I figured out the reason why I made such kind of mistake. Basically it
is a bad practice of writing code.

Usually, I put the type definitions in header files, and then if some
other files want to use these type definitions, I just use the
#include. In this way, the files using these type definition may be
included before the real type definition. So it will give you some
weird error msgs...

A good way (at least I think) to debug this error is to use "gcc -E
*.c", then you can check the preprocessing file very easily:)

For instance,
----A.h-----
#ifndef A_H_
#define A_H_

#include "C.h"

typedef struct{
int x;
int y;
}NODE;

void bar();
#endif

----A.c----
#include "A.h"

void bar(){
foo();
}

----C.h------
#ifndef C_H_
#define C_H_

#include "A.h"

Node foo(); /* need to use type NODE defined in A.h */

#endif

----C.c----
#include "C.h"

Node foo(){}


----main.c----
#include "A.h"
int main(){
bar();
return 1;
}


if you try to compile this code, you will get error msgs like:
-----------------------------------
gcc -O2 -g -Wall -fmessage-length=0 -c -o main.o main.c
In file included from A.h:6,
from main.c:3:
C.h:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before
‘foo’
make: *** [main.o] Error 1
-----------------------------------


Is there some good way to avoid this bad practice? Currently, I just
put these definitions into type.h, which is shared as common header.






.



Relevant Pages