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 8 28168
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
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
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
};
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;
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
| |