如何定义泛型类型的静态变量
how to define a static variable of generic type
我写了一个类如下:
template <class T>
/* Abstract class for stack n queue */
class StacknQueue
{
public:
StacknQueue(int = 10);
virtual int insert(const T&) = 0;
virtual int remove(T&) = 0;
virtual void display() = 0;
int isEmpty() const {return top == -1 || front == -1 || front > top ;}
int isFull() const { return top == size - 1 ;}
//virtual int isQEmpty const() = 0;
//virtual int isQFull const() = 0;
void initialize();
static int flag;
static int size;
//int rear;
static int front;
static int top;
static T *stknqPtr ;
};
当我尝试在类外定义变量stknqPtr时,如下所示:
template <class T>
T StacknQueue<T> :: stknqPtr = new T[size];
显示错误:
error C2040: 'stknqPtr' : 'float' differs in levels of indirection from 'float *'
我是一个c++新手,谁能告诉我怎么做?
Thanks in advance
您的定义类型错误。它没有定义指针。应该是:
template <class T>
T* StacknQueue<T>::stknqPtr = ...;
您需要使其成为正确的类型-在类中是T*
,但在定义中是T
。
但是——你的类设计建议它的所有成员不应该是静态的——否则每个实例都将共享相同的后台数据——这不是你通常想要的堆栈或队列。对于非静态成员,您不需要恼人的显式实例化-因此一切都变得简单。
相关文章:
- 使用泛型类型推送到堆栈时出现问题
- 如何在容器中指定模板化别名的泛型类型
- 具有模板专用化的泛型类型转换
- C++存储泛型 T 类型类的向量
- 如何在C++中返回没有模板的泛型类型?
- 如何在带有约束 (C++) 的函数中使用泛型类型
- 模板函数重载(泛型类型与模板模板类型)选择正确的重载
- 泛型类型别名,它们彼此不兼容
- 泛型类型转换
- 重载泛型类型的模板类时检查运算符=时的自赋值
- 在C++中,如何根据类中的参数返回不同的泛型类型
- C++错误 C2227:'->looseHealth'左侧必须指向类/结构/联合/泛型类型
- 使用泛型类型显式实例化函数模板
- 函数模板中的无效转换错误,返回值取决于其泛型类型
- 如何确保 "<<" 运算符适用于模板化 ADT 定义中的任何泛型类型?
- 仅当泛型类型的值是指针时,才删除该值
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- 用于泛型类型上的泛型操作的SFINAE
- 为什么可以强制转换的泛型类型不会被隐式转换
- 如何定义泛型类型的静态变量