Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

struct & enum & :: visibility operator

5 views
Skip to first unread message

robbio

unread,
Jul 20, 2008, 5:09:31 PM7/20/08
to
Hi at all,

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.

Keith Thompson

unread,
Jul 21, 2008, 7:27:40 PM7/21/08
to
"robbio" <robb...@libero.it> 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) 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"

Barry Schwarz

unread,
Jul 21, 2008, 7:28:06 PM7/21/08
to
On Sun, 20 Jul 2008 16:09:31 -0500 (CDT), "robbio"
<robb...@libero.it> wrote:

snip C++ code

You might have better luck asking C++ questions in a C++ discussion
group.


Remove del for email

Kalle Olavi Niemitalo

unread,
Jul 21, 2008, 7:28:08 PM7/21/08
to
"robbio" <robb...@libero.it> writes:

> 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.

Hans-Bernhard Bröker

unread,
Jul 21, 2008, 7:29:37 PM7/21/08
to
robbio wrote:
> Hi at all,
>
> I have a header file like this:
>
> -------- file.h ------------
> ....
> extern "C"
> {

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.

0 new messages