我有一個類,它有一些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
你有沒有定義兩個靜態變量...... – HadeS
是的。看看'NoBoundsChecking'類。 – puelo
而不是'm_boundsGuard :: sizeFront'使用'BoundsCheckingPolicy :: sizeFront'或'm_boundsGuard.sizeFront' – HadeS