Problem forward declaration of "typedef struct"

  • Thread starter Mohammad Omer Nasir
  • Start date
M

Mohammad Omer Nasir

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

Mohammad Omer Nasir

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
 
S

s5n

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
};
 
M

Mohammad Omer Nasir

Don't need a typedef for that.


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
 
G

Gavin Deane

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
 
M

Marcus Kwok

Gavin Deane said:
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.
 
D

Dave Rahardja

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top