Design issue

  • Thread starter Philipp.Weissenbacher
  • Start date
P

Philipp.Weissenbacher

Hi all!

I've got something of a design issue here:

class Bucket {
public:
// Initial capacity of a bucket (= # of items in a page)
static const unsigned int M = 10;

// Key store
Key keyStore[M];

// Count of items in the bucket
int count;

// Bucket's depth (number of significant bits)
int localDepth;

Bucket();
~Bucket();
};

Now, I use Bucket in a data structure like this:

#include "Bucket.h"

Foobar::Foobar() : a(0), b(0), c(0) {
}

void Foobar buzz() {
if(Bucket::M < 1)
...
}

Although this compiles just fine, if I want to use it e.g. write
something like

#include "Foobar.h"
int maint() {
Foobar* b = new Foobar();
}

I get a "undefined reference" from ld. Googling on this I found out
that I have one has to initialize a static variable in exactly one
compilation unit (see [10.10] Why can't I initialize my static member
data in my constructor's initialization list? by Marshall Cline).
But how do I fix this?

Thanks in advance,
Philipp
 
E

Erik Wikström

Hi all!

I've got something of a design issue here:

class Bucket {
public:
// Initial capacity of a bucket (= # of items in a page)

static const unsigned int M;
// Key store
Key keyStore[M];

// Count of items in the bucket
int count;

// Bucket's depth (number of significant bits)
int localDepth;

Bucket();
~Bucket();
};

In Bucket.cpp add:

Bucket::M = 10;

I get a "undefined reference" from ld. Googling on this I found out
that I have one has to initialize a static variable in exactly one
compilation unit (see [10.10] Why can't I initialize my static member
data in my constructor's initialization list? by Marshall Cline).
But how do I fix this?

The answer can be found in item 10.11 in the same FAQ.
 

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,780
Messages
2,569,611
Members
45,267
Latest member
WaylonCogb

Latest Threads

Top