2014-03-02 49 views
0

我有一個類,它有一些static const變量作爲模板類模板的問題。 這是我的普通類(就在header文件):用作模板的類中的靜態常量變量

class NoBoundsChecking 
{ 
public: 
    static const size_t sizeFront = 0; 
    static const size_t sizeBack = 0; 

    inline void guardFront() const {}; 
}; 

這是我要如何使用它(也是header文件):

template<class BoundsCheckingPolicy> 
class MemoryArena 
{ 
public: 

    MemoryArena() 
    { 
    } 

    void* allocate(size_t size, size_t alignment, int line, char* file) 
    { 
     size_t boundedSize = m_boundsGuard::sizeFront + size + m_boundsGuard::sizeBack; 
     m_boundsGuard.guardFront(); 
    } 

private: 
    BoundsCheckingPolicy m_boundsGuard; 
}; 

這工作得很好:m_boundsGuard.guardFront(); 但這m_boundsGuard::sizeFront給我錯誤。 下面是完整的錯誤:

error C2653: 'm_boundsGuard' : is not a class or namespace name 
1>e:\...\memorymanager.h(102) : while compiling class template member function 'void *MemoryArena<NoBoundsChecking>::allocate(size_t,size_t,int,char *)' 
1>e:\...\main.cpp(21) : see reference to function template instantiation 'void *MemoryArena<NoBoundsChecking>::allocate(size_t,size_t,int,char *)' being compiled 
1>e:\...\main.cpp(19) : see reference to class template instantiation 'MemoryArena<NoBoundsChecking>' being compiled 
1>e:\...\memorymanager.h(111): error C2065: 'sizeFront' : undeclared identifier 
+0

你有沒有定義兩個靜態變量...... – HadeS

+0

是的。看看'NoBoundsChecking'類。 – puelo

+0

而不是'm_boundsGuard :: sizeFront'使用'BoundsCheckingPolicy :: sizeFront'或'm_boundsGuard.sizeFront' – HadeS

回答

4

m_boundsGuard不是類或命名空間。正確的版本是:

// Using dot 
size_t boundedSize = m_boundsGuard.sizeFront + size + m_boundsGuard.sizeBack; 

// Using class 
size_t boundedSize = BoundsCheckingPolicy::sizeFront + size + BoundsCheckingPolicy::sizeBack; 
+0

哦,我的天啊。我怎麼能看不到這個。非常感謝你的回答。我會盡快將你的答案標記爲正確的,因爲你是第一! – puelo

2

您正在試圖通過一個對象來訪問靜態成員,而不是類。試試這個:

size_t boundedSize = BoundsCheckingPolicy::sizeFront + size + BoundsCheckingPolicy::sizeBack;