使用具有一些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 :(得分: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;
答案 1 :(得分:2)
您正尝试通过对象而不是类访问静态成员。试试这个:
size_t boundedSize = BoundsCheckingPolicy::sizeFront + size + BoundsCheckingPolicy::sizeBack;