Boost compile error when a object of type pool is contained in another class

Discussion in 'C++' started by mackenzie, Jun 13, 2007.

  1. mackenzie

    mackenzie Guest

    Hello,
    I am looking for a little bit of help. I am trying to create a
    dynamically allocated object which contains one or more objects of
    type boost::pool<>. I get a compiler error when an object of this type
    is contained in my class and am not sure why. To be honest I have a
    little but not a lot of experience with templates and it could simply
    be obvious to a more experienced template user; however, the answer
    escapes me.

    Here is a sample code snippet:

    #include <boost/pool/pool.hpp>
    #include <boost/pool/singleton_pool.hpp>

    struct Foobar
    {
    boost::pool<> p(sizeof(int)); // this is line 182

    };

    boost::pool<> yp(512);

    int main()
    {
    Foobar foobar;

    printf( "%u\n", sizeof( boost::pool<>(512) ) );

    return( 0 );
    }

    ----------------------
    Here is the error message:
    ramMgr.cxx:182: error: expected identifier before #sizeof#
    ramMgr.cxx:182: error: expected #,# or #...# before #sizeof#
    ---------------------------
    compiler & version
    g++ -v
    gcc version 4.1.1 20060724 (4.1.1-3mdk)


    Thanks,
    Parker
    mackenzie, Jun 13, 2007
    #1
    1. Advertising

  2. mackenzie wrote:
    > Hello,
    > I am looking for a little bit of help. I am trying to create a
    > dynamically allocated object which contains one or more objects of
    > type boost::pool<>. I get a compiler error when an object of this type
    > is contained in my class and am not sure why. To be honest I have a
    > little but not a lot of experience with templates and it could simply
    > be obvious to a more experienced template user; however, the answer
    > escapes me.
    >
    > Here is a sample code snippet:
    >
    > #include <boost/pool/pool.hpp>
    > #include <boost/pool/singleton_pool.hpp>
    >
    > struct Foobar
    > {
    > boost::pool<> p(sizeof(int)); // this is line 182


    What's the 'sizeof' for? Are you trying to initialise it? If so,
    initialisation of members belongs to constructor initialiser list.
    If you were trying to declare an array, then replace parentheses
    with brackets. Most likely you just need to lose the parentheses
    and the expression inside them.

    >
    > };
    >
    > boost::pool<> yp(512);
    >
    > int main()
    > {
    > Foobar foobar;
    >
    > printf( "%u\n", sizeof( boost::pool<>(512) ) );
    >
    > return( 0 );
    > }
    >
    > ----------------------
    > Here is the error message:
    > ramMgr.cxx:182: error: expected identifier before #sizeof#
    > ramMgr.cxx:182: error: expected #,# or #...# before #sizeof#
    > ---------------------------
    > compiler & version
    > g++ -v
    > gcc version 4.1.1 20060724 (4.1.1-3mdk)
    >
    >
    > Thanks,
    > Parker


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 13, 2007
    #2
    1. Advertising

  3. mackenzie

    mackenzie Guest

    On Jun 13, 4:54 pm, "Victor Bazarov" <> wrote:
    > mackenzie wrote:
    > > Hello,
    > > I am looking for a little bit of help. I am trying to create a
    > > dynamically allocated object which contains one or more objects of
    > > type boost::pool<>. I get a compiler error when an object of this type
    > > is contained in my class and am not sure why. To be honest I have a
    > > little but not a lot of experience with templates and it could simply
    > > be obvious to a more experienced template user; however, the answer
    > > escapes me.

    >
    > > Here is a sample code snippet:

    >
    > > #include <boost/pool/pool.hpp>
    > > #include <boost/pool/singleton_pool.hpp>

    >
    > > struct Foobar
    > > {
    > > boost::pool<> p(sizeof(int)); // this is line 182

    >
    > What's the 'sizeof' for? Are you trying to initialise it? If so,
    > initialisation of members belongs to constructor initialiser list.
    > If you were trying to declare an array, then replace parentheses
    > with brackets. Most likely you just need to lose the parentheses
    > and the expression inside them.
    >
    >
    >
    >
    >
    > > };

    >
    > > boost::pool<> yp(512);

    >
    > > int main()
    > > {
    > > Foobar foobar;

    >
    > > printf( "%u\n", sizeof( boost::pool<>(512) ) );

    >
    > > return( 0 );
    > > }

    >
    > > ----------------------
    > > Here is the error message:
    > > ramMgr.cxx:182: error: expected identifier before #sizeof#
    > > ramMgr.cxx:182: error: expected #,# or #...# before #sizeof#
    > > ---------------------------
    > > compiler & version
    > > g++ -v
    > > gcc version 4.1.1 20060724 (4.1.1-3mdk)

    >
    > > Thanks,
    > > Parker

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Thanks for responding.

    What I thought I was doing was creating a member of Foobar named p of
    type boost::pool<> which takes a size_type as an argument to the
    constructor. It seems to work in the global name space; however, when
    I put it into a structure or class I get the error.

    >From : http://www.boost.org/libs/pool/doc/interfaces/pool.html

    template <typename UserAllocator = default_user_allocator_new_delete>
    class pool
    {...
    explicit pool(size_type requested_size);
    };

    Perhaps after the drive home and a clearer head I will stare at it
    some more with your suggestion in mind and the answer will be a little
    more obvious.

    Thanks,
    Parker
    mackenzie, Jun 14, 2007
    #3
  4. mackenzie wrote:
    > On Jun 13, 4:54 pm, "Victor Bazarov" <> wrote:
    >> mackenzie wrote:
    >>> Hello,
    >>> I am looking for a little bit of help. I am trying to create a
    >>> dynamically allocated object which contains one or more objects of
    >>> type boost::pool<>. I get a compiler error when an object of this
    >>> type is contained in my class and am not sure why. To be honest I
    >>> have a little but not a lot of experience with templates and it
    >>> could simply be obvious to a more experienced template user;
    >>> however, the answer escapes me.

    >>
    >>> Here is a sample code snippet:

    >>
    >>> #include <boost/pool/pool.hpp>
    >>> #include <boost/pool/singleton_pool.hpp>

    >>
    >>> struct Foobar
    >>> {
    >>> boost::pool<> p(sizeof(int)); // this is line 182

    >>
    >> What's the 'sizeof' for? Are you trying to initialise it? If so,
    >> initialisation of members belongs to constructor initialiser list.
    >> If you were trying to declare an array, then replace parentheses
    >> with brackets. Most likely you just need to lose the parentheses
    >> and the expression inside them.
    >> [..]

    >
    > What I thought I was doing was creating a member of Foobar named p of
    > type boost::pool<> which takes a size_type as an argument to the
    > constructor.


    You're trying to provide a particular argument in a declaration.
    That's not how you initialise the member. Please read about
    constructors (of classes) and the _member_initialiser_lists_.

    > It seems to work in the global name space; however, when
    > I put it into a structure or class I get the error.
    >
    >> From : http://www.boost.org/libs/pool/doc/interfaces/pool.html

    > template <typename UserAllocator = default_user_allocator_new_delete>
    > class pool
    > {...
    > explicit pool(size_type requested_size);
    > };
    >
    > Perhaps after the drive home and a clearer head I will stare at it
    > some more with your suggestion in mind and the answer will be a little
    > more obvious.
    >
    > Thanks,
    > Parker


    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 14, 2007
    #4
  5. mackenzie

    mackenzie Guest

    Thank you Victor.

    That makes perfect sense. I was looking at it as if it was being
    passed in as a template argument. When in fact it is similar to any
    other class.

    Thanks again,
    Parker
    mackenzie, Jun 14, 2007
    #5
  6. mackenzie

    mackenzie Guest

    On Jun 14, 7:34 am, mackenzie <> wrote:
    > Thank you Victor.
    >
    > That makes perfect sense. I was looking at it as if it was being
    > passed in as a template argument. When in fact it is similar to any
    > other class.
    >
    > Thanks again,
    > Parker


    For completeness, in case someone else has a similar question in the
    future, the following is the corrected code:

    struct Foobar
    {
    boost::pool<> p; // this was line 182

    Foobar() : p( 5 ) {}
    };

    Reference: 10.4.6.1 Necessary Member Initialization; The C++
    Programming Language; Bjarne Stroustrup; May 2004

    Thanks again Victor.
    mackenzie, Jun 14, 2007
    #6
    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. Chanchal
    Replies:
    2
    Views:
    462
  2. Adam Hartshorne
    Replies:
    1
    Views:
    347
    Victor Bazarov
    Mar 3, 2005
  3. Faheem Mitha
    Replies:
    2
    Views:
    663
    Faheem Mitha
    Aug 26, 2006
  4. werasm

    boost pool per class...

    werasm, Nov 17, 2007, in forum: C++
    Replies:
    9
    Views:
    635
    James Kanze
    Nov 18, 2007
  5. Rick Lawson
    Replies:
    8
    Views:
    779
    Graham Dumpleton
    Jul 17, 2009
Loading...

Share This Page