need help about template class

Discussion in 'C++' started by yichao.zhang, Oct 19, 2006.

  1. yichao.zhang

    yichao.zhang Guest

    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!
     
    yichao.zhang, Oct 19, 2006
    #1
    1. Advertising

  2. yichao.zhang

    mlimber Guest

    yichao.zhang wrote:
    > 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
     
    mlimber, Oct 19, 2006
    #2
    1. Advertising

  3. yichao.zhang

    yichao.zhang Guest

    thanks mlimber.
    the link you post is very useful too.
     
    yichao.zhang, Oct 19, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page