Re: struct & enum & :: visibility operator



"robbio" <robbio72@xxxxxxxxx> writes:
Hi at all,

I have a header file like this:

-------- file.h ------------
....
extern "C"

extern "C" is a syntax error in C. Apparently this header is meant to
be C++, which is a different language with its own newsgroups.

It's common to use something like this:

#ifdef __cplusplus
extern "C" {
#endif

/* ... */

#ifdef __cplusplus
}
#endif

if you really feel the need to use the same header for both C and C++.

{
...
typedef _S S;

This attempts to declare "S" as an alias for the type "_S". You
haven't declared a type called "_S", so of course the compiler doesn't
know what you're talking about. Things don't become visible until
*after* you declare them.

Well, mostly. You *could* have declared

typedef struct _S S;

This introduces "struct _S" as an incomplete type; you can't declare
an object of that type, but you can declare a typedef or a pointer
type that uses it.

...

struct _S

And, in C, this doesn't declare a type "_S"; it declares a type
"struct _S". Unlike in C++, you can't refer to "struct _S" as just
"_S".

Also, it's dangerous to use identifiers starting with an underscore;
such identifiers are reserved to the implementation. The rule is
actually a little more complicated, but that's all you need to
remember: don't declare things using identifiers starting with
underscores.

You don't really *need* the typedef at all. You can just declare your
type as:

struct S { /* ... */ };

and refer to it as "struct S". Or, if you prefer to have a one-word
name for the type, you can use the same identifier for the typedef as
for the struct tag:

typedef struct S { /* ... */ } S;

If you need to refer to the type name inside the declaration, things
are a little more complicated; I'll leave the details for later.


--
Keith Thompson (The_Other_Keith) kst-u@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"
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.



Relevant Pages

  • Re: Exported function mangaled name
    ... extern "C" { ... You have otherwise defined a function unrelated to the header ... You have to declare the function the same way in your .cpp file as in your .h file ... #define LIBSPEC __declspec ...
    (microsoft.public.vc.mfc)
  • Re: Namespaces: Initializing variables
    ... You must declare not define the variable in the ... For declaration in the header, use extern: ... extern DWORD avar; ...
    (microsoft.public.vc.language)
  • Re: Newbie Classes & externals
    ... Venturing into the world of C# from MFC. ... I could declare the class ... > as 'extern' in the app's header, and then every other class that had the ... app's header could use the same class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Newbie Classes & externals
    ... I could declare the class ... as 'extern' in the app's header, and then every other class that had the app's header could use the same class. ... Ray K. ...
    (microsoft.public.dotnet.languages.csharp)
  • where is the template instance?
    ... I have a header that includes this short definition: ... typedef size_t weight_type; ... extern def_rand_type std_rand; ... It both compiles and runs. ...
    (alt.comp.lang.learn.c-cpp)