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

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

  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. 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. Nagaraj
    Replies:
    1
    Views:
    842
    Lionel B
    Mar 1, 2007
  2. timjowers
    Replies:
    7
    Views:
    4,312
    heyjude
    Feb 2, 2011
  3. Replies:
    1
    Views:
    438
    Cowboy \(Gregory A. Beamer\)
    Apr 11, 2008
  4. Carter
    Replies:
    2
    Views:
    490
    Carter
    Mar 4, 2009
  5. fAnSKyer
    Replies:
    2
    Views:
    519
    Alf P. Steinbach
    Jun 7, 2009
Loading...

Share This Page