亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下代碼中,為什么總是編譯失敗,而且結構體里面那個template T 不識別!

以下代碼中,為什么總是編譯失敗,而且結構體里面那個template T 不識別!

C++
墨色風雨 2022-04-20 14:11:36
/*2.a 實現棧模板類要求:1)用鏈表形式來存儲棧數據2)實現棧的常用函數*/#include <iostream>using namespace std;template <class T>class Mystack{  struct Node  { T data;  Node *next;  };   Node *head;public:Mystack(){head=NULL;}~Mystack(){while(head!=NULL){Node*p=head;head=p->next;delete p;}}void input(){cout<<"開始輸入數據"<<endl;T x;cin>>x;while(x!=-1){Node *p=new Node ;p->data=x;p->next=head;head=p;cin>>x;}}T push(T n){T num=n;Node *p=new Node;p->data=num;p->next=NULL;if(head==NULL){head=p;return num;}else{p->next=head;head=p;return num;}}T pop(){if(head==NULL)return -1;else{T x=head->data;Node *p=head;head=head->next;delete p;return x;}}void display(){Node *p=head;for(;p!=NULL;p=p->next)cout<<p->data<<" ";cout<<endl;}void gettop(){if(head==NULL)cout<<"the stack is empty!"<<endl;elsecout<<"the top number is "<<head->data<<endl;}bool empty(){if(head==NULL)return true;elsereturn false;}};void main(){Mystack<int> h;cout<<"開始輸入數據(為int型)"<<endl;h.input();h.gettop();int x;cout<<"輸入進棧的數據(為int型)"<<endl;cin>>x;if(h.push(x)==x)cout<<"進棧成功!"<<endl;cout<<"進棧之后的數據輸出:"<<endl;h.display();if(!(h.pop()==-1)){cout<<"出棧成功!"<<endl;cout<<"出棧之后的數據輸出:"<<endl;h.display();}elsecout<<"棧內沒有數據"<<endl;cout<<endl;cout<<"**************************************"<<endl;Mystack<double> t;cout<<"開始輸入數據(為double型)"<<endl;t.input();t.gettop();double y;cout<<"輸入進棧的數據(為double型)"<<endl;cin>>y;if(t.push(y)==y)cout<<"進棧成功!"<<endl;cout<<"進棧之后的數據輸出:"<<endl;t.display();if(!(t.pop()==-1)){cout<<"出棧成功!"<<endl;cout<<"出棧之后的數據輸出:"<<endl;t.display();}elsecout<<"棧內沒有數據"<<endl;}
查看完整描述

2 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

T是指一種類型 比如string 你只寫T當然不會識別了

查看完整回答
反對 回復 2022-04-24
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

在結構體的那里也加上、template<typename T>

查看完整回答
反對 回復 2022-04-24
  • 2 回答
  • 0 關注
  • 159 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號