aggregate initialisation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raghu

    aggregate initialisation

    Hello all,
    Can somebody help me hopw to resolve teh probelm of aggregate initialisation
    in c++.
    Her eis the piece of code.

    #include<stdio. h>

    class MyTest
    {
    public:
    DECLARE_AGGREGA TABLE (MyTest);
    MyTest(float input = 0.0f):Data(inpu t){}
    float Data;
    };

    struct MyStruct
    {
    MyTest a;
    int b;
    };
    struct MyStruct test[2] = { {1.2f,1},{2.4f, 3}};

    int main()
    {

    printf("%f\n",t est[0].a.Data);
    return 0;
    }

    I am getting the compialtion error as follows:

    error C2440: 'initializing' : cannot convert from 'const float' to 'struct
    MyStruct'
    No constructor could take the source type, or constructor overload
    resolution was ambiguous.

    And when i decalrea str4uct variable as follows:

    struct MyStruct test = { 1.2f ,3 };
    I am getting the following error:

    error C2552: 'test' : non-aggregates cannot be initialized with initializer
    list

    Is there any option to use initaliser list toassign the UDTS aggregately?
    Your help is needed at the earliest.

    Thanks in Advance.
    Raghu






  • benben

    #2
    Re: aggregate initialisation

    Raghu wrote:[color=blue]
    > Hello all,
    > Can somebody help me hopw to resolve teh probelm of aggregate initialisation
    > in c++.[/color]

    What is aggregate initialization anyway? Aggregation can have a lot of
    meanings in different contexts, which one are you talking about?
    [color=blue]
    > Her eis the piece of code.
    >
    > #include<stdio. h>
    >
    > class MyTest
    > {
    > public:
    > DECLARE_AGGREGA TABLE (MyTest);[/color]

    Well, what is DECLARE_AGGREGA TEABLE? Either provide the definition of
    that macro or provide the resultant (non-macro) code equivalent.
    [color=blue]
    > MyTest(float input = 0.0f):Data(inpu t){}
    > float Data;
    > };
    >
    > struct MyStruct
    > {
    > MyTest a;
    > int b;
    > };
    > struct MyStruct test[2] = { {1.2f,1},{2.4f, 3}};[/color]

    MyStruct test[] = {{MyTest(1.2f), 1}, {MyTest(2.4f), 3}};
    [color=blue]
    >
    > int main()
    > {
    >
    > printf("%f\n",t est[0].a.Data);
    > return 0;
    > }
    >
    > I am getting the compialtion error as follows:
    >
    > error C2440: 'initializing' : cannot convert from 'const float' to 'struct
    > MyStruct'
    > No constructor could take the source type, or constructor overload
    > resolution was ambiguous.
    >
    > And when i decalrea str4uct variable as follows:
    >
    > struct MyStruct test = { 1.2f ,3 };[/color]

    MyStruct test = {MyTest(1.2f), 3};
    [color=blue]
    > I am getting the following error:
    >
    > error C2552: 'test' : non-aggregates cannot be initialized with initializer
    > list
    >
    > Is there any option to use initaliser list toassign the UDTS aggregately?
    > Your help is needed at the earliest.[/color]

    I still don't get why you need a macro for this matter but if you just
    change the lines to those I wrote it should be all right...
    [color=blue]
    >
    > Thanks in Advance.
    > Raghu
    >[/color]

    Regards,
    Ben

    Comment

    • Ron Natalie

      #3
      Re: aggregate initialisation

      Raghu wrote:[color=blue]
      > Hello all,
      > Can somebody help me hopw to resolve teh probelm of aggregate initialisation
      > in c++.
      > Her eis the piece of code.
      >
      > #include<stdio. h>
      >
      > class MyTest
      > {
      > public:
      > DECLARE_AGGREGA TABLE (MyTest);[/color]

      What does the above line mean?

      Comment

      • Ron Natalie

        #4
        Re: aggregate initialisation

        benben wrote:[color=blue]
        > Raghu wrote:[color=green]
        >> Hello all,
        >> Can somebody help me hopw to resolve teh probelm of aggregate
        >> initialisation
        >> in c++.[/color]
        >
        > What is aggregate initialization anyway? Aggregation can have a lot of
        > meanings in different contexts, which one are you talking about?
        >[/color]
        The C++ standard defines aggregate. MyTest is not an aggregate (has
        a user-defined constructor). However MyStruct is an aggregate. A
        member that is not an aggregate itself is permitted in an aggregate.
        (It's not like POD's where everything contained within must also be
        POD).

        Comment

        Working...