Boost logo

Boost Users :

From: Daryle Walker (darylew_at_[hidden])
Date: 2005-09-03 17:48:20


On 9/3/05 9:56 AM, "Fujinobu Takahashi" <fjtaka_at_[hidden]> wrote:

> Hello Cory-san,
>
> Thank you for your prompt advice for my basic question.
> I found same thing happens even when I use dynamic bitsets as member
> of struct.

I think you need to review your C++ syntax.

> My original simple code:
[SNIP]
> struct multi_bitsets{
> dynamic_bitset<> db1(10,1ul);
> dynamic_bitset<> db2(10,2ul);
> };
[SNIP]
> multi_bitset_struct.org.cpp:9: error: expected identifier before numeric
> constant
> multi_bitset_struct.org.cpp:9: error: expected `,' or `...' before numeric
> constant
> multi_bitset_struct.org.cpp:9: error: ISO C++ forbids declaration of
> `parameter' with no type
> multi_bitset_struct.org.cpp:10: error: expected identifier before numeric
> constant
> multi_bitset_struct.org.cpp:10: error: expected `,' or `...' before numeric
> constant
> multi_bitset_struct.org.cpp:10: error: ISO C++ forbids declaration of
> `parameter' with no type
[SNIP]

You can never initialize members of a struct/class in that fashion. You
must use constructors:

    class multi_bitsets
    {
    public:
        multi_bitsets() : db1(10, 1ul), db2(10, 2ul) {}
        //...

    private:
        dynamic_bitset<> db1, db2;
    }

Or you can initialize a simple struct/class at declaration time:

    struct test1 { int a, b };
    //...
    int main() {
        test1 x = { 1, 4 };
        //...
    }

I forgot what restrictions, if any, are on that feature. If a class member
is of static storage and of a built-in integral type, then you can do:

    class test2
    {
        static unsigned const xx = 2u;
        //...
    }

    //...

    unsigned const test2::xx; // You still must do this!

(I forgot if it works on "enum" too.)

> -------------------------------------------------------------------
> And modified code with the initializer list suggested by Cory-san:
[SNIP]
> struct multi_bitsets{
> dynamic_bitset<> db1;
> dynamic_bitset<> db2;
> };
> multi_bitsets():db1(10,1ul), db2(10,2ul){}
[SNIP]
> multi_bitset_struct.cpp:14: error: expected unqualified-id before ')' token
> multi_bitset_struct.cpp:14: error: expected `,' or `;' before ':' token

Not modified enough. All members of a class must be declared in the class
body, during or before a definition (if any). You cannot define an
impromptu constructor (i.e. with no prior declaration).

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT hotmail DOT com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net