How to compile the code?

Discussion in 'C++' started by ctick, Jun 18, 2004.

  1. ctick

    ctick Guest

    I try to make a member of "dynamic_bitset" of a dynamic size that is
    determined when its containing class is instantiated. The code has error
    when compiling: "N is undefined" something like that. Obviously, N is
    defined before x!

    Does anyone know how to solve the problem?

    Thanks in advance!

    class bset
    {
    private:
    int N;
    boost::dynamic_bitset<> x(N);
    public:
    bset(int n);
    };

    bset::bset() : N(n)
    {
    }

    void main()
    {
    bset bs;
    ...
    }
     
    ctick, Jun 18, 2004
    #1
    1. Advertisements

  2. "ctick" <> wrote in message
    news:fqvAc.807$...
    > I try to make a member of "dynamic_bitset" of a dynamic size that is
    > determined when its containing class is instantiated. The code has error
    > when compiling: "N is undefined" something like that. Obviously, N is
    > defined before x!
    >
    > Does anyone know how to solve the problem?
    >
    > Thanks in advance!
    >
    > class bset
    > {
    > private:
    > int N;
    > boost::dynamic_bitset<> x(N);
    > public:
    > bset(int n);
    > };
    >
    > bset::bset() : N(n)
    > {
    > }
    >
    > void main()
    > {
    > bset bs;
    > ...
    > }
    >
    >
    >
     
    John Harrison, Jun 18, 2004
    #2
    1. Advertisements

  3. Sorry hit reply too early. Answer below.

    "ctick" <> wrote in message
    news:fqvAc.807$...
    > I try to make a member of "dynamic_bitset" of a dynamic size that is
    > determined when its containing class is instantiated. The code has error
    > when compiling: "N is undefined" something like that. Obviously, N is
    > defined before x!
    >
    > Does anyone know how to solve the problem?
    >
    > Thanks in advance!
    >
    > class bset
    > {
    > private:
    > int N;
    > boost::dynamic_bitset<> x(N);


    Lose the (N)

    boost::dynamic_bitset<> x;

    > public:
    > bset(int n);
    > };
    >
    > bset::bset() : N(n)


    Replace with this

    bset::bset() : N(n), x(n)

    > {
    > }
    >


    Strange one, you knew you could write N(n), but you didn't know to write
    x(n)?

    Another point, there is no need to store the size separately, if you want
    the size of a dynamic_bitset you can just say x.size(). If you store the
    size separately you are wasting space and even more importantly there is
    always the chance that the size of the bitset and the size in N will get out
    of step.

    So change the code to this

    class bset
    {
    private:

    boost::dynamic_bitset<> x;
    public:
    bset(int n);
    int get_size() const;
    };

    bset::bset(int n) : x(n)
    {
    }

    int bset::get_size() const
    {
    return x.size();
    }

    john
     
    John Harrison, Jun 18, 2004
    #3
    1. Advertisements

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. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,799
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Profetas

    Code to compile a code

    Profetas, Jul 27, 2004, in forum: C Programming
    Replies:
    7
    Views:
    347
    Christopher Benson-Manica
    Jul 27, 2004
  3. Nagaraj
    Replies:
    1
    Views:
    1,184
    Lionel B
    Mar 1, 2007
  4. timjowers
    Replies:
    7
    Views:
    4,711
    heyjude
    Feb 2, 2011
  5. Replies:
    1
    Views:
    598
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  6. Carter
    Replies:
    2
    Views:
    677
    Carter
    Mar 4, 2009
  7. fAnSKyer
    Replies:
    2
    Views:
    712
    Alf P. Steinbach
    Jun 7, 2009
  8. Justin Ezequiel
    Replies:
    4
    Views:
    248
    Gregory Ewing
    Apr 26, 2014
Loading...