Re: templates and inheritance

From: James Dennett (jdennett_at_acm.org)
Date: 03/16/04


Date: Mon, 15 Mar 2004 21:04:09 -0800

son, you bore me wrote:

> James Dennett wrote:
>
> <snipped>
>
>>If you change the NewClass<Comparable> constructor from taking
>>Comparable& as an argument to taking Comparable const&, you
>>might find that this works better.
>>
>>The following will compile:
>>
>>std::string const& r("zzconst");
>>
>>-- James.
>
>
> Thanks for your suggestion. Unfortunately, that has not solved my problem.

It solved the problem you had identified, it appears. That's
a good thing. Now you can move on to solving the next one.

> My NewClass constructor looks like this:
>
> .cpp file
> template<class Comparable>
> NewClass<Comparable>::NewClass(const Comparable& param) : BaseClass(param){}
>
> .h file
> NewClass(const Comparable& param);
>
>
> I even went in my test program and changed how the parameter was given to
> constructor:
>
> const string x = "ZZZZ";
> NewClass<string> test(x);
>
> And yes, I did try the NewClass constructor as NewClass(Comparable const&
> param). I got the same problem no matter which way I did it. Here is the
> output of the compiler:
>
> /tmp/ccM7iwya.o(.gnu.linkonce.t._ZN12NewClassISsEC1ERKSs+0x10): In function
> `NewClass<std::basic_string<char, std::char_traits<char>,
> std::allocator<char> > >::NewClass[in-charge](std::basic_string<char,
> std::char_traits<char>, std::allocator<char> > const&)':
> : undefined reference to `BaseClass<std::basic_string<char,
> std::char_traits<char>, std::allocator<char> >
>
>>::AvlTree[not-in-charge](std::basic_string<char, std::char_traits<char>,
>
> std::allocator<char> > const&)'
> /tmp/ccM7iwya.o(.gnu.linkonce.t._ZN12NewClassISsED1Ev+0xd): In function
> `NewClass<std::basic_string<char, std::char_traits<char>,
> std::allocator<char> > >::~NewClass [in-charge]
> : undefined reference to `BaseClass<std::basic_string<char,
> std::char_traits<char>, std::allocator<char> > >::~BaseClass
> [not-in-charge]()'
> collect2: ld returned 1 exit status
>
> One thing that comes quickly to mind, is the reference to ::~NewClass
> and ::~BaseClass. These are destructors, which I have not declared, nor
> implemented in my NewClass.

The compiler has declared them for you, as it must if they are
not explicitly declared. And when you call them, it should
instantiate them. How that instantiation gets linked into your
final code depends on your compiler (which looks like g++ in
this case -- at least I assume you've used g++, not gcc).

> If anybody has an idea, I'm all ears.

Put the template code in the header file, not in a separate
source file, unless your compiler supports the "export"
keyword used for (a kind of) separate compilation of
templates. I don't know for sure if that will solve
your problem, but your second problem is one of linkage,
not one of code not compiling.

-- James.



Relevant Pages

  • Re: VC++ Compiler issue
    ... > My case might fit in "moderately complicated template code" that you ... > If you have any idea why the compiler is producing an error message, ... >>> I am trying to migrate a Linux project to a Windows project. ... >>> Duke ...
    (microsoft.public.vsnet.general)
  • Re: templates and inheritance
    ... > Put the template code in the header file, ... with Gnu Compiler 3.2.2 on a command line (side note, ... to my laptop, added the same line to the copy of my NewClass.cpp file, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How protect source code in template library?
    ... > prevent others from getting .cpp files. ... To instantiate template code for new data types, ... the compiler does need to see the template source code (which ... (while the way to call C-style functions is pretty much ...
    (comp.lang.cpp)
  • Re: objects? we dont need no stinking objects!
    ... specific language. ... The compiler doesn't know how many objects you will ... instantiate when you compile the code. ... In terms of dynamic arrays under MV ...
    (comp.databases.pick)
  • Re: Assignable Expression Templates?
    ... keeping in mind, though, is that if the compiler can instantiate your ... >> There are many who believe that the auto keyword should be overloaded ... I wouldn't mind it, of course. ...
    (comp.lang.cpp)