在文件 SomeClass.h
#ifndef SOME_CLASS_H_
#define SOME_CLASS_H_
#include <iostream>
using std::cout;
using std::endl;
template <class T, class P>
class SomeClass
{
public:
SomeClass();
void SomeMethod();
protected:
typedef unsigned int heapPosition;
heapPosition someVariable;
private:
static const heapPosition NULLPOSITION;
};
template <class T, class P>
const typename SomeClass<T,P>::heapPosition SomeClass<T,P>::NULLPOSITION = -1;
template <class T, class P>
SomeClass<T,P>::SomeClass(){}
template <class T, class P>
void SomeClass<T,P>::SomeMethod()
{
someVariable=NULLPOSITION;
cout<<"NULLPOSITION:"<<NULLPOSITION<<endl;
}
#endif
在文件 main.cpp
#include <cstdlib>
#include <iostream>
#include "SomeClass.h"
using namespace std;
int main(int argc, char *argv[])
{
SomeClass<int,int> someClass;
someClass.SomeMethod();
system("PAUSE");
return EXIT_SUCCESS;
}
基本上我有一个带有静态 const 成员的模板类(NULLPOSITION)
。我已经尝试了类的 inizialitazion,包括类定义之外和内联,如
static const heapPosition NULLPOSITION=-1;
声明成员时。
然而,在这两种情况下,当我引用SomeMethod
它的值时,它的值是一些随机值——即它还没有被初始化。
这种事情我做过很多次了,从来没有遇到过这种问题。
我究竟做错了什么?
有人能帮帮我吗?非常感谢您抽出宝贵时间。
谢谢,杰拉德·塞伦特