templates and inheritance

G

Guest

I am writing a container class so that I can reuse my double ended list
code. I have a base class CListBase that handles inserts and deletes with a
member function addItem, defined as:
template<class T>
class CListBase
{
CListBase<T>::CListBase();
virtual CListBase<T>::~CListBase();
void addItem ( T item);
...
};

when I create a derived class to store entries :
class CSymList : public CListBase<class CSymlist Entry*> { ... } ;

and try to compile I get linker errors "unresolved external symbol" for the
constructor, destructor. and member function addItem. What am I missing? -gsw
 
C

Carl Daniel [VC++ MVP]

Greg said:
I am writing a container class so that I can reuse my double ended
list

Why bother? Use std::list instead - it's already there for you.
code. I have a base class CListBase that handles inserts and deletes
with a member function addItem, defined as:
template<class T>
class CListBase
{
CListBase<T>::CListBase();
virtual CListBase<T>::~CListBase();
void addItem ( T item);
...
};

when I create a derived class to store entries :
class CSymList : public CListBase<class CSymlist Entry*> { ... } ;

and try to compile I get linker errors "unresolved external symbol"
for the constructor, destructor. and member function addItem. What
am I missing? -gsw

Remember that templates are not code. Rather, they're recipes that the
compiler uses to generate code. Without seeing more of how your code is
structured, I can't tell you exactly what's wrong, but typically this comes
up when someone tries to put the template declaration in a header file and
the implementation of the methods in a .cpp file. When that .cpp file is
compiled, nothing of the template appears in the resulting .obj file unless
the template was instantiated in that .cpp file.

The only practical solution with most C++ compilers, VC++ included, is to
put all of the implementation of your template class into the header file
(or into a file #included by the header file) so that the entire template
definition is visible to the compiler at the point of use (since that's
where the compiler actually generates code).

HTH

-cd
 
I

Ismail Pazarbasi

The compiler version makes sense. Also, the "style" can cause that. The
ISO C++ standard doesn't concern your style. It is, as far as I know,
legal, but never seen in standard headers, nor in examples in standard
book. It is legal, but "class CSymlist Entry*" doesn't look nice.
Classes are already types. So, CSymlistEntry itself is already a type
that you can use as a template argument.

VC6.0 cannot link, if methods of a template class are defined in a cpp
file. Exporting is not supported on that version. I am not sure about
VC7.0, but VC7.1 can link, if methods are defined in cpp.

Using std::list is not that brilliant in all cases. May be he doesn't
like to use CRT? Or may be he needs something different? I would, for
example, recommend, ATL's CSimpleList.

As Carl said, many compilers love to see implementations of template
member functions in the very same header file, not in somewhere else.

Ismail
 
C

Carl Daniel [VC++ MVP]

Ismail said:
The compiler version makes sense. Also, the "style" can cause that.
The ISO C++ standard doesn't concern your style. It is, as far as I
know, legal, but never seen in standard headers, nor in examples in
standard book. It is legal, but "class CSymlist Entry*" doesn't look
nice. Classes are already types. So, CSymlistEntry itself is already
a type that you can use as a template argument.

VC6.0 cannot link, if methods of a template class are defined in a cpp
file. Exporting is not supported on that version. I am not sure about
VC7.0, but VC7.1 can link, if methods are defined in cpp.

No, it cannot. The linker sees only template instantiations, and the
compiler only instantiates templates in the translation units where they're
used. So, if you put the template definition in a .cpp file AND instantiate
the template for all combinations of template parameters that you use in
that translation unit, then the program will link. That's true for every
version of VC that supports templates.
Using std::list is not that brilliant in all cases. May be he doesn't
like to use CRT? Or may be he needs something different? I would, for
example, recommend, ATL's CSimpleList.

As Carl said, many compilers love to see implementations of template
member functions in the very same header file, not in somewhere else.

All compilers except Comeau, to be more precise.

-cd
 

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

Top