By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Problem forward declaration of "typedef struct"

P: n/a
Hi,

i made a structure in header file "commonstructs.h" is:

typedef struct A
{
int i;

A( )
{
i = 90;
}
} AA, * LPAA;

after I wrote structure code, i made a class with the name "CTemp",
which have a function with the name of fun1(AA *) and following code
was written in Temp.h Header file...

class CTemp
{
public:
void fun1( AA * temp);
};

and following code was written in Temp.cpp File...

#include "Temp.h"
#include "commonstructs.h"

void CTemp::fun1( AA * temp )
{
cout<<temp->i<<endl;
}

when i tried to write forward declaration code of structure A in
Temp.h header file which is

struct A;
class CTemp
{
public:
void fun1( AA * temp);
};

i compiled this code on vc++ 2k5 and it gives the following error:

error C2371: 'A' : redefinition; different basic types
error C2512: 'A' : no appropriate default constructor available

but if i write following line for forward declaration of struct A then
its work fine..

typedef struct A AA, *LPAA;

is the nature of structure A is change by making structure with
typedef??

Regards,

-aims

Feb 19 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Feb 19, 11:21 am, "Mohammad Omer Nasir" <momer...@gmail.comwrote:
Hi,

i made a structure in header file "commonstructs.h" is:

typedef struct A
{
int i;

A( )
{
i = 90;
}

} AA, * LPAA;

after I wrote structure code, i made a class with the name "CTemp",
which have a function with the name of fun1(AA *) and following code
was written in Temp.h Header file...

class CTemp
{
public:
void fun1( AA * temp);

};

and following code was written in Temp.cpp File...

#include "Temp.h"
#include "commonstructs.h"

void CTemp::fun1( AA * temp )
{
cout<<temp->i<<endl;

}

when i tried to write forward declaration code of structure A in
Temp.h header file which is

struct A;
class CTemp
{
public:
void fun1( AA * temp);

};

i compiled this code on vc++ 2k5 and it gives the following error:

error C2371: 'A' : redefinition; different basic types
error C2512: 'A' : no appropriate default constructor available

but if i write following line for forward declaration of struct A then
its work fine..

typedef struct A AA, *LPAA;

is the nature of structure A is change by making structure with
typedef??
You don't need the typedef in C++, struct A { ... }; will do. Then you
can forward declare it with just struct A;.

--
Erik Wikström

Feb 19 '07 #2

P: n/a
You don't need the typedef in C++, struct A { ... }; will do. Then you
can forward declare it with just struct A;.
But i defined AA specifically for making Objects and LPAA for
specifically pointer of A structure... i want to know the behavior of
"typedef struct" type structures, when we need to forward declaration
of it...

regards,

-aims

Feb 19 '07 #3

P: n/a
s5n
On Feb 19, 12:05 pm, "Mohammad Omer Nasir" <momer...@gmail.comwrote:
But i defined AA specifically for making Objects and LPAA for
specifically pointer of A structure... i want to know the behavior of
"typedef struct" type structures, when we need to forward declaration
of it...
i can't answer that question, but i feel compelled to point out that
what you did here:
when i tried to write forward declaration code of structure A
in Temp.h header file which is
....

is NOT a forward declaration. You redefined the whole class. A forward
declaration takes ONLY the name of the class and no members, like
this:

class CTemp;

But there are limitations of what a forward decl can do. For example,
if you only have a type available via a forward decl then it is an
"incomplete type" and you cannot call functions on it:

class Foo;
....
Foo foo = Foo(); // illegal because Foo ctor not visible
....
Foo * foo = new Foo(); // same
....
foo->bar(); // illegal because Foo::bar() is not visible

But a fwd decl is okay for some purposes, like declaring a function
which takes a pointer or reference to that type:

void myFunction( CTemp const & ); // legal

or as part of a class:

class Foo;
class XYZ {
....
private:
Foo * m_foo; // legal
Foo m_bar; // NOT legal because Foo is incomplete
};

Feb 19 '07 #4

P: n/a
Mohammad Omer Nasir wrote:
>You don't need the typedef in C++, struct A { ... }; will do. Then you
can forward declare it with just struct A;.

But i defined AA specifically for making Objects
Don't need a typedef for that.
and LPAA for specifically pointer of A structure...
Why? What's wrong with A*? Anyway, you can still do:

typedef A* LPAA;

Feb 19 '07 #5

P: n/a
can forward declare it with just struct A;.
But i defined AA specifically for making Objects

Don't need a typedef for that.
and LPAA for specifically pointer of A structure...

Why? What's wrong with A*? Anyway, you can still do:

typedef A* LPAA;
ok lets look at this, this code taken from winnt.h header file

typedef struct _RTL_CRITICAL_SECTION {
PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

LONG LockCount;
LONG RecursionCount;
HANDLE OwningThread;
HANDLE LockSemaphore;
ULONG_PTR SpinCount;

} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

y is this developer declaring _RTL_CRITICAL_SECTION and
RTL_CRITICAL_SECTION and *PRTL_CRITICAL_SECTION, when he could also
have done what u told me? Becuz it lets us declare a lot of types in
one declaration. The same thing is what I'm trying to achieve and I'm
getting an error and I want to understand y i'm getting an error. I
can figure out the workarounds (and I have, u also gave a solution)
but first I >want< to understand whats happening in the current
situation.

regards,

-aims

Feb 19 '07 #6

P: n/a
On 19 Feb, 14:10, "Mohammad Omer Nasir" <momer...@gmail.comwrote:
>can forward declare it with just struct A;.
But i defined AA specifically for making Objects
Don't need a typedef for that.
and LPAA for specifically pointer of A structure...
Why? What's wrong with A*? Anyway, you can still do:
typedef A* LPAA;

ok lets look at this, this code taken from winnt.h header file

typedef struct _RTL_CRITICAL_SECTION {
PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

LONG LockCount;
LONG RecursionCount;
HANDLE OwningThread;
HANDLE LockSemaphore;
ULONG_PTR SpinCount;

} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

y is this developer declaring _RTL_CRITICAL_SECTION and
RTL_CRITICAL_SECTION and *PRTL_CRITICAL_SECTION, when he could also
have done what u told me?
Yes. Because the typedef syntax is (or used to be) necessary in C. I
can't remember exactly why, but it has never been necessary in C++.
Becuz it lets us declare a lot of types in one declaration.
Not really. The only extra thing you get is PRTL_CRITICAL_SECTION
which you can use to obfuscate the present of a pointer.
The same thing is what I'm trying to achieve and I'm
getting an error and I want to understand y i'm getting an error. I
can figure out the workarounds (and I have, u also gave a solution)
but first I >want< to understand whats happening in the current
situation.
Your original post contained this

typedef struct A
{
int i;

A( )
{
i = 90;
}

} AA, * LPAA;

Now, to remove some unnecessary confusion, replace the above with

struct A
{
int i;

A( )
{
i = 90;
}
};

Replace *all* occurances of AA with A and replace *all* occurances of
LPAA with A*. Other than making your code easier to read, this should
have *no effect* on your problem. But it will make reading the code to
work out what's wrong easier.

Gavin Deane

Feb 19 '07 #7

P: n/a
Gavin Deane <de*********@hotmail.comwrote:
On 19 Feb, 14:10, "Mohammad Omer Nasir" <momer...@gmail.comwrote:
>typedef struct _RTL_CRITICAL_SECTION {
PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

LONG LockCount;
LONG RecursionCount;
HANDLE OwningThread;
HANDLE LockSemaphore;
ULONG_PTR SpinCount;

} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

y is this developer declaring _RTL_CRITICAL_SECTION and
RTL_CRITICAL_SECTION and *PRTL_CRITICAL_SECTION, when he could also
have done what u told me?

Yes. Because the typedef syntax is (or used to be) necessary in C. I
can't remember exactly why, but it has never been necessary in C++.
As far as I know, the typedef trick in C is used so that you do not need
to use the keyword "struct" when declaring objects of that type. For
example, given the definition:

struct RTL_CRITICAL_SECTION {
/* members */
};

In C, you would have to declare an object of this type as:

struct RTL_CRITICAL_SECTION r;

whereas in C++, you can simply do:

RTL_CRITICAL_SECTION r;

The typedef trick allows you to simplify the C syntax to look like the
C++ syntax.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 19 '07 #8

P: n/a
On 19 Feb 2007 06:10:53 -0800, "Mohammad Omer Nasir" <mo******@gmail.com>
wrote:
>ok lets look at this, this code taken from winnt.h header file

typedef struct _RTL_CRITICAL_SECTION {
PRTL_CRITICAL_SECTION_DEBUG DebugInfo;

LONG LockCount;
LONG RecursionCount;
HANDLE OwningThread;
HANDLE LockSemaphore;
ULONG_PTR SpinCount;

} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

y is this developer declaring _RTL_CRITICAL_SECTION and
RTL_CRITICAL_SECTION and *PRTL_CRITICAL_SECTION, when he could also
have done what u told me? Becuz it lets us declare a lot of types in
one declaration. The same thing is what I'm trying to achieve and I'm
getting an error and I want to understand y i'm getting an error. I
can figure out the workarounds (and I have, u also gave a solution)
but first I >want< to understand whats happening in the current
situation.
The pattern you're seeing in winnt.h is a concession to the way C deals with
the names of structs. It is not necessary to do that in C++, and it is viewed
as an anachronism.

For instance, the following code is legal in C++, but not in C:
struct S
{
/* ... */
};

S s;
In C, the definition of s must be:
struct S s;
To avoid the confusion, C authors tend to write:
typedef struct S
{
/* ... */
} S;
Declaring "lots of types" in one declaration is not necessarily a good thing.
Why generate a new set of symbols whose naming conventions you have to follow,
when there are perfectly legitimate and convenient language constructs that
mean the same thing?

Look at the code:

typedef struct _RTL_CRITICAL_SECTION { /* ... */
} RTL_CRITICAL_SECTION, *PRTL_CRITICAL_SECTION;

You have defined the symbols

_RTL_CRITICAL_SECTION
RTL_CRITICAL_SECTION
PRTL_CRITICAL_SECTION

When all you need is

RTL_CRITICAL_SECTION

A pointer to that structure is simply RTL_CRITICAL_SECTION*.
Also, a typedef is not an alias for the original type. So you cannot do this:
struct S { /* ... */ };
typedef S A;

struct A; // <-- Error. A is a typedef, not an alias for S.
And finally, please use whole English words when asking your questions. "y",
"u", and "becuz" are not. You'll find that people tend to help you more in
this group when you do.

-dr
Feb 19 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.