473,657 Members | 2,567 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 28453
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_S ECTION {
PRTL_CRITICAL_S ECTION_DEBUG DebugInfo;

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

} RTL_CRITICAL_SE CTION, *PRTL_CRITICAL_ SECTION;

y is this developer declaring _RTL_CRITICAL_S ECTION and
RTL_CRITICAL_SE CTION 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_S ECTION {
PRTL_CRITICAL_S ECTION_DEBUG DebugInfo;

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

} RTL_CRITICAL_SE CTION, *PRTL_CRITICAL_ SECTION;

y is this developer declaring _RTL_CRITICAL_S ECTION and
RTL_CRITICAL_SE CTION 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_S ECTION
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*********@ho tmail.comwrote:
On 19 Feb, 14:10, "Mohammad Omer Nasir" <momer...@gmail .comwrote:
>typedef struct _RTL_CRITICAL_S ECTION {
PRTL_CRITICAL_S ECTION_DEBUG DebugInfo;

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

} RTL_CRITICAL_SE CTION, *PRTL_CRITICAL_ SECTION;

y is this developer declaring _RTL_CRITICAL_S ECTION and
RTL_CRITICAL_S ECTION 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_SE CTION {
/* members */
};

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

struct RTL_CRITICAL_SE CTION r;

whereas in C++, you can simply do:

RTL_CRITICAL_SE CTION 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_S ECTION {
PRTL_CRITICAL_S ECTION_DEBUG DebugInfo;

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

} RTL_CRITICAL_SE CTION, *PRTL_CRITICAL_ SECTION;

y is this developer declaring _RTL_CRITICAL_S ECTION and
RTL_CRITICAL_S ECTION 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_S ECTION { /* ... */
} RTL_CRITICAL_SE CTION, *PRTL_CRITICAL_ SECTION;

You have defined the symbols

_RTL_CRITICAL_S ECTION
RTL_CRITICAL_SE CTION
PRTL_CRITICAL_S ECTION

When all you need is

RTL_CRITICAL_SE CTION

A pointer to that structure is simply RTL_CRITICAL_SE CTION*.
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
18976
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 class to be able to use all kinds of datasets (2D/3D/4D...) ----------------------------------------------------------- class dataset_base{ public: virtual void printDataInfo()=0;
10
3509
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 struct foobar; ^
2
34544
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
4300
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; REKORD1 buff ; and then read record from file with read (file, (void *) &buff, sizeof (REKORD1));
0
1649
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 'CCmdLineParser_String'", and "C2061: syntax error : identifier 'LPCTSTR'" shows in compiling. What is the problem? Following is the header of the class: #if _MSC_VER > 1000 #pragma once
4
2478
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 something like this: MyOtherClass<T> Type; but let's keep it simple for the moment */
8
40463
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 familiar with "typedef int NUMBER", but how does it work with structures. Tilak
11
5641
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 to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this code, I'd like to know that also. I realize there is overhead with using an "accessor" function. ...
4
171385
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 enum without typedef. In some places they are using typedef before enumeration definition. Kindly provide some reference, for why they are using typedef.
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.