Re: templates and inheritance
From: son, you bore me (nope_at_nada.net)
Date: 03/18/04
- Next message: Dan Moos: "Re: My final shape program, what do you think?"
- Previous message: ed: "Re: [c++]creating your own namespaces"
- In reply to: James Dennett: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 17 Mar 2004 19:56:58 -0500
James Dennett wrote:
> 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.
I would like to thank you for your help. I finally got it to work, and in
keeping with my "Don't ask, don't tell" policy, I won't tell you how I did
it, because quite frankly, I have no clue. But I will explain what I did
do. I tried the template code in the header file in various layouts, and
got various errors. I use Linux on my desktop, and I use Kate text editor
with Gnu Compiler 3.2.2 on a command line (side note, yes I am using the
g++ command), but on my laptop is Windows and I use JGrasp with Cygwin, and
Cygwin uses the Gnu Compiler 3.2. So I tried the files on it, with no
template code in the header file, and the error code I got was "ISO C++
forbids declaration with no 'type'" (may not be exact error message). The
alarm bells in my head went off. Usually when I get this error message I
did something stupid, like trying to use a string variable, but forgetting
to #include <string>. I went back to my desktop, and I opened up the files
in a program called KDevelop instead of Kate. In my NewClass.cpp file, I
added #include "BaseClass.cpp". Compiled the program, and wouldn't you know
it, the damn thing worked. I can create an object with the NewClass
constructor, and all the base class functions have been inherited. Now
comes the part why I say I have no idea what fixed the problem. I went back
to my laptop, added the same line to the copy of my NewClass.cpp file,
compiled it, and got a big list of errors. Rechecked both sets of copies.
They are exactly the same. Took another copy off my desktop, shoved the
files in a different folder on my laptop, compiled, and it works. I now
have two exact copies of the same files on my laptop, one set will compile
and run, and the other nada. And people always ask me why my forehead looks
bruised whenever I start messing with C++.
- Next message: Dan Moos: "Re: My final shape program, what do you think?"
- Previous message: ed: "Re: [c++]creating your own namespaces"
- In reply to: James Dennett: "Re: templates and inheritance"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|