Maz
Maz

Reputation: 3375

Static class variable--use with constructors

I have a class with a static variable: null.

static Pointer<Value> Null;

Pointer is a class which uses reference count memory management.

However, I get an error: no matching function for call to Pointer::Pointer()

On the line:

Pointer<Value> Value::Null(new Value());

Thanks.

Excerpt of Pointer class:

template <typename T>
class Pointer
{
 public:
  explicit Pointer(T* inPtr);

Constructor Source

  mPtr = inPtr;
  if (sRefCountMap.find(mPtr) == sRefCountMap.end()) {  
    sRefCountMap[mPtr] = 1;
  } else {
    sRefCountMap[mPtr]++;
  }

Upvotes: 1

Views: 194

Answers (3)

Vite Falcon
Vite Falcon

Reputation: 6645

I believe it should be like:

// In the header file.
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// In the cpp file.
const Pointer<Value> Value::Null(reinterpret_cast<Value*>(0));

Upvotes: 0

stinky472
stinky472

Reputation: 6797

I'm assuming your class is named Value.

// Header file
class Value
{
public:
    ...
    static const Pointer<Value> Null;
};

// This should be in the cpp file.
const Pointer<Value> Value::Null(new Value);

Upvotes: 0

Gianni
Gianni

Reputation: 4390

The line:

static Pointer<Value> Null;

Is call the Pointer::Pointer() constructor. It apears that your Pointer class does not have such a constructor, but instead has a constructor that accepts void*. So try changing it to

static Pointer<Value> Null(0);

Upvotes: 1

Related Questions