need help about template class

Y

yichao.zhang

Hi all
when I wrote this 2 lines code in one file, it compiles fine.

template <class T> class A {};
class B: A<int> {};

however
when I put this code in one file,
template <class T> class A {};

this code in another file
template <class T> class A;
class B: A<int> {};

it complains that
M:\zz\clang>g++ tem.cpp tem2.cpp
tem2.cpp:3: base class `A<int>' has incomplete type

M:\zz\clang>g++ -v
Reading specs from D:/MinGW/bin/../lib/gcc-lib/mingw32/3.2.3/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld
--with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls
--enable
-languages=c++,f77,objc --disable-win32-registry --disable-shared
--enable-sjlj-
exceptions
Thread model: win32
gcc version 3.2.3 (mingw special 20030504-1)


what does this mean? "base class `A<int>' has incomplete type"
any lights much appreciated!
 
M

mlimber

yichao.zhang said:
Hi all
when I wrote this 2 lines code in one file, it compiles fine.

template <class T> class A {};
class B: A<int> {};

however
when I put this code in one file,
template <class T> class A {};

this code in another file
template <class T> class A;
class B: A<int> {};

it complains that
M:\zz\clang>g++ tem.cpp tem2.cpp
tem2.cpp:3: base class `A<int>' has incomplete type
[...]
what does this mean? "base class `A<int>' has incomplete type"
any lights much appreciated!

It means that you have *declared* A<> in the second file but have not
*defined* it. To use the class (e.g., by inheriting from it or creating
an instance), it needs to be *defined* in the current translation unit.
Probable fix: include the first file (which is presumably a header
file, since g++ doesn't support export as far as I know; cf.
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.14) in the
second.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top