473,508 Members | 2,040 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem forward declaration of "typedef struct"

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
8 28428
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
18961
by: Joel Kullberg | last post by:
Hi! I have a question for u guys! I would like to use the c++ package ITK (www.itk.org) for internal handling och data and functions in a dataset3D class of mine! I also want to use a base...
10
3486
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern...
2
34492
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
3
4293
by: Pawe³ | last post by:
Hi! I want to read with C# some binary file with well defined binary structure. In C++ I was declaring appropriate struct, like: typedef struct {BYTE b1; WORD w1, w2; DWORD dw1} REKORD1;...
0
1635
by: Gary | last post by:
Hi, there, I am going to use a command line parser class, which works well in VC6.0, in VC++7.1. However error message such as "C2059: syntax error :')'", and "C2061: syntax error : identifier...
4
2461
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need...
8
40447
by: cman | last post by:
What does this kind of typedef accomplish? typedef struct { unsigned long pte_low; } pte_t; typedef struct { unsigned long pgd; } pgd_t; typedef struct { unsigned long pgprot; } pgprot_t I am...
11
5623
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
4
171292
by: msukumarbabu | last post by:
Hi all, What will be difference between "typedef enum" and "enum". or difference between “typedef structure" and "structure" I am going through some code. in that some place they are using...
0
7226
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.