gpt4 book ai didi

c++ - 模板函数参数有什么问题

转载 作者:太空宇宙 更新时间:2023-11-04 11:26:42 27 4
gpt4 key购买 nike

您好,我是模板新手。只是想知道如何正确编译程序。

 template<class t>
class node{
public:
t val;
node(t v):val(v){}
};
template<class t>
class stack{
private:
stack *next;
static stack *head;
static int top;
public:
void push(node *n);
node* pop();
};
template<class t>
int stack<t>::top=0;
template<class t>
stack<t>* stack<t>::head=NULL;
template<class t>
void stack<t>::push(node<t>* n) //Error the push function is not defined properly
{

}


int main(int argc, char *argv[])
{
node<int> n1(5);
return 0;
}

程序报错

  stack<t>::push' : redefinition; different basic types
nw.cpp(14) : see declaration of 'stack<t>::push'

提前致谢

最佳答案

类模板node需要模板参数

使用 node<t>在:

void push(node<t> *n);node<t>* pop();并根据实现

关于c++ - 模板函数参数有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26407932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com