Re: templates and inheritance
From: James Dennett (jdennett_at_acm.org)
Date: 03/16/04
- Next message: James Dennett: "Re: Std::multiset's real application"
- Previous message: John Cho: "My final shape program, what do you think?"
- In reply to: son, you bore me: "Re: templates and inheritance"
- Next in thread: son, you bore me: "Re: templates and inheritance"
- Reply: son, you bore me: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: James Dennett: "Re: Std::multiset's real application"
- Previous message: John Cho: "My final shape program, what do you think?"
- In reply to: son, you bore me: "Re: templates and inheritance"
- Next in thread: son, you bore me: "Re: templates and inheritance"
- Reply: son, you bore me: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|