How to compile the code?

C

ctick

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;
...
}
 
J

John Harrison

ctick said:
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;
...
}
 
J

John Harrison

Sorry hit reply too early. Answer below.

ctick said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top