2010-06-28 93 views
1

我有一個包含靜態變量的類:null。靜態類變量 - 與構造函數一起使用

static Pointer<Value> Null; 

指針是一個使用引用計數內存管理的類。

不過,我得到一個錯誤:調用指針不匹配函數::指針()

就行了:

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

感謝。

指針類的摘錄:

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

構造來源

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

這是定義在全局範圍的源文件? – 2010-06-28 13:14:30

+0

你能告訴我們更多的代碼嗎?你的構造函數中發生了什麼? – wheaties 2010-06-28 13:15:19

+0

'Null'成員屬於哪一類? - 另外我認爲可能有更好的方法去做你想做的事情,例如來自Boost或Boost.Optional的智能指針類。 – Philipp 2010-06-28 13:20:53

回答

0

我假設您的課程名爲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); 
+0

我仍然得到相同的錯誤,如果我嘗試 – Maz 2010-06-28 13:49:16

+2

假設是否不正確?你可以用實際的Null靜態成員顯示你的Value類嗎? – stinky472 2010-06-28 13:51:10

+0

還要確保指針模板的類定義包含在cpp文件中,並且該值本身包含在內。 – stinky472 2010-06-28 13:51:50

1

行:

static Pointer<Value> Null; 

是調用指針::指針()構造函數。它指出你的指針類沒有這樣的構造函數,而是有一個接受void*的構造函數。因此,請嘗試將其更改爲

static Pointer<Value> Null(0); 
+0

我仍然得到這樣的錯誤。 – Maz 2010-06-28 13:46:11

+0

看着你現在發佈的代碼,問題是你聲明你的構造函數是'explicit'。這意味着要麼刪除'explicit'關鍵字,要麼使用'reinterpret_cast <0>'或爲這種情況創建'void *'的新的顯式受保護構造函數。 – Gianni 2010-06-28 17:04:57

0

我相信它應該是這樣的:

// 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)); 
+0

我仍然得到這樣的錯誤。 – Maz 2010-06-28 14:30:35