Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template的奇怪問題...

0 views
Skip to first unread message

四尾

unread,
Jul 28, 1998, 3:00:00 AM7/28/98
to
==> 在 deu...@bbs.ee.ntu.edu.tw (Harold) 的文章中提到:
: 請問在template下的constructor是不是一定要inline?
: 我有以下的小程式 a.h a.cc testa.cc
: // a.h
: template <class Type>
: class A {
: public: A(int size);
: Type *ia;
: };
: // a.cc
: #include "a.h"
: template <class Type>
: A<Type>::A(int size)
: { ia = new Type[size];
: }
: // testa.cc
: #include "a.h"
: main()
: { A<int> ra(2);
^^^^^^^^^^^^^^
: }
: 在link時總是有undefined symbol, 找不到 constructor
: 在a.h內寫成inline就沒問題
: 請問是有哪裡錯了嗎?
: compiler g++ 2.8.1


1.你所寫的 template 內容要在這個地方才會被 complier
2.在 testa.cc 之中,你沒有把 a.cc include 進來,所以在 compiler 的
時候 contructor 並沒有被處理,所以在 link 的時候變成 undefined symbol
3.把 template 的成員函數獨立成一個檔案是沒有必要的,記得 1. 中所說
4.如果一定要這樣作的話,記得在 a.h(或 testa.cc) 中加上
#include <a.cc>

Harold

unread,
Jul 28, 1998, 3:00:00 AM7/28/98
to
==> 在 iw....@csie.nctu.edu.tw (四尾) 的文章中提到:

> 1.你所寫的 template 內容要在這個地方才會被 complier
> 2.在 testa.cc 之中,你沒有把 a.cc include 進來,所以在 compiler 的
> 時候 contructor 並沒有被處理,所以在 link 的時候變成 undefined symbol
> 3.把 template 的成員函數獨立成一個檔案是沒有必要的,記得 1. 中所說
> 4.如果一定要這樣作的話,記得在 a.h(或 testa.cc) 中加上
> #include <a.cc>

在 C++ Primer(3rd edition) 書中提到, template 不支援 separation compilation
model, 必須加 export 才可
但似乎 g++ 2.8.1 還沒支援 export

--
* Origin: ★ 交通大學資訊科學系 BBS ★ <bbs.cis.nctu.edu.tw: 140.113.23.3>

0 new messages