[問題] inheritance與template結合的作業問題
如版規不合規定請告知,馬上修改
爬了一下文
稍微了解了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
2 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
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
10/13 11:59, 4F
→
10/13 12:02, , 5F
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
10/13 12:06, 9F
→
10/13 12:19, , 10F
10/13 12:19, 10F
推
10/13 14:05, , 11F
10/13 14:05, 11F
推
10/13 15:36, , 12F
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