Template class pointer c++ declaration

前端 未结 5 1797
误落风尘
误落风尘 2021-01-30 23:42
template 
class Node
{...};

int main
{
    Node* ptr;
    ptr = new Node;
}

Will fail to compile I have to to declare the

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-31 00:11

    As both Neil Butterworth and Luc Danton noted, you can't have a pointer of type Node* because Node is not a type. It is a template. Yes, Node is a class template, but that class here just qualifies the kind of template. One way to look at it: Just as classes and instances of classes are very different things, so too are templates and template instantiations. It is those template instantiations such as Node that are classes. The class template is some other kind of beast.

提交回复
热议问题