I have a header file like this:
-------- file.h ------------
....
extern "C"
{
...
typedef _S S;
...
struct _S
{
...
enum
{
a,
b
}myenum;
...
};
}
--------- end of file.h -------------
and a C file like this
----------- file.c ----------------
.....
#include "file.h"
static void myFunc(S* pS)
{
....
if (... == _S::a)
....
....
}
-----------end of file.c --------------
I'm using the Microsoft 2005 environment and so its compiler. The compiler
says me that there is an error in myFunc, the error is that _S is an
undeclared indentifier. I'm not an expert of C but I don't know where the
problem is.
Can anyone help me, please?
thanks a lot
roberto
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- 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.
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) ks...@mib.org <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"
snip C++ code
You might have better luck asking C++ questions in a C++ discussion
group.
Remove del for email
> extern "C"
> {
C does not support extern "C". Put #ifdef __cplusplus around
that and the braces.
#ifdef __cplusplus
extern "C"
{
#endif
...
#ifdef __cplusplus
}
#endif
> ...
> typedef _S S;
> ...
>
> struct _S
_S is a reserved identifier because it begins with an underscore
and a capital letter. Use some other name, for example S.
Besides, the typedef already must specify that it refers to a
struct type.
typedef struct S S;
...
struct S
{
...
};
That will work in C++ as well.
> static void myFunc(S* pS)
> {
> ....
> if (... == _S::a)
C does not support a :: operator.
Although you defined the enum inside the struct,
C places the constants of the enum in the global scope,
so you can just use a and b here.
if (... == a)
You should therefore choose longer names that are less likely
to conflict.
As shown, this problem is manifestly off-topic. There's no 'extern "C"'
in the C programming language.
Your problem is with C++, not C. Yes, that's a different language.