Initialize struct with struct 
Author Message
 Initialize struct with struct

I'm trying to initialize a structure (with a member
variable of type struct).  It seems the compiler )
VC++.NET won't let me.  Can anyone shed some light?  I
can't find any documentation on this topic in online-help,
Stroustrup, or anywhere else.

Thanks

typedef struct tagINNERSTRUCT {
  char icOne;
  int  iiTwo;

Quote:
} INNERSTRUCT;

typedef struct tagOUTERSTRUCT {
  INNERSTRUCT   oInner;

Quote:
} OUTERSTRUCT;

INNERSTRUCT inner = { 1, 2 };
OUTERSTRUCT w;
w.oInner = inner;                              // OK
OUTERSTRUCT y = { inner.icOne, inner.iiTwo };  // OK
OUTERSTRUCT x = { inner };                     // error
C2440: 'initializing' : cannot convert from 'INNERSTRUCT'
to 'char'


Thu, 15 Dec 2005 02:08:10 GMT  
 Initialize struct with struct

Quote:

>I'm trying to initialize a structure (with a member
>variable of type struct).  It seems the compiler )
>VC++.NET won't let me.  Can anyone shed some light?  I
>can't find any documentation on this topic in online-help,
>Stroustrup, or anywhere else.

>Thanks

>typedef struct tagINNERSTRUCT {
>  char icOne;
>  int  iiTwo;
>} INNERSTRUCT;

>typedef struct tagOUTERSTRUCT {
>  INNERSTRUCT       oInner;
>} OUTERSTRUCT;

>INNERSTRUCT inner = { 1, 2 };
>OUTERSTRUCT w;
>w.oInner = inner;                              // OK
>OUTERSTRUCT y = { inner.icOne, inner.iiTwo };  // OK
>OUTERSTRUCT x = { inner };                     // error
>C2440: 'initializing' : cannot convert from 'INNERSTRUCT'
>to 'char'

VC++ .NET 2003 (aka VC 7.1) fixes a couple of problems with aggregate
initialization, including this one. BTW, in C++ there's no need to use the
typedef method above to declare classes; just use:

struct X
{
   ...

Quote:
};

(If you were talking about C, the rules might very well be different, but
what you've presented is legal C++.)

--
Doug Harrison
Microsoft MVP - Visual C++



Thu, 15 Dec 2005 04:20:44 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Initialize array from members of a struct: Legal ?

2. Initializing const auto structs

3. padding in a statically initialized struct

4. Initializing an array of structs

5. how to initialize a struct

6. Initializing an Array of struct

7. Syntax for initializing structs

8. initializing pointer in struct with address of auto identifier

9. Initializing a pointer inside a struct

10. initialize a struct..

11. Initializing. a huge array of structs

12. Initializing. a huge array of structs

 

 
Powered by phpBB® Forum Software