[問題] inheritance與template結合的作業問題

看板C_and_CPP作者 (saponevol23)時間15年前 (2010/10/13 11:40), 編輯推噓6(6011)
留言17則, 10人參與, 最新討論串1/1
如版規不合規定請告知,馬上修改 爬了一下文 稍微了解了inheritance跟template的融合 也試著寫了一下,可是出現錯誤訊息 目的是用Inheritance跟template寫出類似stack的資料型態 此stack可以接受多種資料型態(如int float等) 我的寫法是 template <class TL> class list { . . . }; template <class TL> class stack: TL { . . . } 原意是用stack去繼承list 在main裡 int main() { list<int> *s1; stack<int> s; s1 = &s; . . . . } 但compile時出現以下錯誤訊息: in instantiation of 'stack<int>': instantiated from here base type 'int' fails to be struct or class type In function 'int main()': 's1' undeclared(first use this function) (Each undeclared identifier is reported only once for each function it appears in.) 因為是初學template,不知道問題出在哪邊 1. 用了template不是就可將各種型態輸入嗎?   還是我必須在class stack做相關設定呢? 2. s1在main裡一開始不是就宣告了嗎?   為什麼會顯示undeclared?是方法有誤嗎? 如果問了白癡問題請多包涵ˊˋ 我也試了蠻久才硬著頭皮上來問問題的 以上疑問,希望有人能幫忙解答 ,感謝! -- ┬┬ ┬┬ ┬┬ ┬┬ ┬┬ Fisher Bryant Artest Gasol Bynum Los Angeles     LAKERS   24 37 16 17 2010 │││ ζacenova -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.118.46 ※ 編輯: saponevol23 來自: 140.116.118.46 (10/13 11:40)

10/13 11:51, , 1F
你要繼承 list 應該是 class stack: public list<TL> 吧
10/13 11:51, 1F

10/13 11:56, , 2F
我試試
10/13 11:56, 2F

10/13 11:59, , 3F
過了耶 囧 可是結果怪怪的 我再看一下
10/13 11:59, 3F

10/13 11:59, , 4F
這樣這篇文要D掉嗎?
10/13 11:59, 4F

10/13 12:02, , 5F
請不要自D
10/13 12:02, 5F

10/13 12:03, , 6F
修改文章告訴大家你已經解決問題、如何解決 會比較好
10/13 12:03, 6F

10/13 12:05, , 7F
嗯嗯,我把作業做完時會上來改
10/13 12:05, 7F

10/13 12:05, , 8F
主要是怕問題太白癡有礙觀瞻="=
10/13 12:05, 8F

10/13 12:06, , 9F
沒有白癡的問題的,很可能別人也會遇到 XD
10/13 12:06, 9F

10/13 12:19, , 10F
就算是會被罵的問題,留下來也能避免下一個人來挨罵。
10/13 12:19, 10F

10/13 14:05, , 11F
我都加在發文提示裡面了 還問要不要D 同學你太緊張了喔XD
10/13 14:05, 11F

10/13 15:36, , 12F
拜託問問題不要自D....
10/13 15:36, 12F

10/13 20:45, , 13F
誰沒菜過...
10/13 20:45, 13F

10/13 20:58, , 14F
我菜了十年
10/13 20:58, 14F

10/13 21:00, , 15F
我也沒什麼信心找程式工程師的工作
10/13 21:00, 15F

10/14 07:22, , 16F
同一樓上
10/14 07:22, 16F

10/14 07:22, , 17F
10/14 07:22, 17F
文章代碼(AID): #1CjIeKbE (C_and_CPP)