aggregate initialisation

Discussion in 'C++' started by Raghu, May 23, 2006.

  1. Raghu

    Raghu Guest

    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_AGGREGATABLE (MyTest);
    MyTest(float input = 0.0f):Data(input){}
    float Data;
    };

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

    int main()
    {

    printf("%f\n",test[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
     
    Raghu, May 23, 2006
    #1
    1. Advertising

  2. Raghu

    benben Guest

    Raghu wrote:
    > Hello all,
    > Can somebody help me hopw to resolve teh probelm of aggregate initialisation
    > in c++.


    What is aggregate initialization anyway? Aggregation can have a lot of
    meanings in different contexts, which one are you talking about?

    > Her eis the piece of code.
    >
    > #include<stdio.h>
    >
    > class MyTest
    > {
    > public:
    > DECLARE_AGGREGATABLE (MyTest);


    Well, what is DECLARE_AGGREGATEABLE? Either provide the definition of
    that macro or provide the resultant (non-macro) code equivalent.

    > MyTest(float input = 0.0f):Data(input){}
    > float Data;
    > };
    >
    > struct MyStruct
    > {
    > MyTest a;
    > int b;
    > };
    > struct MyStruct test[2] = { {1.2f,1},{2.4f,3}};


    MyStruct test[] = {{MyTest(1.2f), 1}, {MyTest(2.4f), 3}};

    >
    > int main()
    > {
    >
    > printf("%f\n",test[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 };


    MyStruct test = {MyTest(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.


    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...

    >
    > Thanks in Advance.
    > Raghu
    >


    Regards,
    Ben
     
    benben, May 23, 2006
    #2
    1. Advertising

  3. Raghu

    Ron Natalie Guest

    Raghu wrote:
    > 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_AGGREGATABLE (MyTest);


    What does the above line mean?
     
    Ron Natalie, May 23, 2006
    #3
  4. Raghu

    Ron Natalie Guest

    benben wrote:
    > Raghu wrote:
    >> Hello all,
    >> Can somebody help me hopw to resolve teh probelm of aggregate
    >> initialisation
    >> in c++.

    >
    > What is aggregate initialization anyway? Aggregation can have a lot of
    > meanings in different contexts, which one are you talking about?
    >

    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).
     
    Ron Natalie, May 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rickman
    Replies:
    3
    Views:
    5,022
    rickman
    Jul 15, 2003
  2. aggregate operator

    , Jul 11, 2005, in forum: VHDL
    Replies:
    3
    Views:
    1,413
  3. jens
    Replies:
    1
    Views:
    686
    Mike Treseler
    Oct 6, 2005
  4. Olaf Kittelmann

    J2EE aggregate entity - Value Disassembler

    Olaf Kittelmann, Dec 2, 2003, in forum: Java
    Replies:
    0
    Views:
    395
    Olaf Kittelmann
    Dec 2, 2003
  5. Tim Clacy
    Replies:
    8
    Views:
    422
    Tim Clacy
    May 30, 2006
Loading...

Share This Page