By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,369 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

Template class inheritance problem

P: n/a
Hi. I have a problem with a template class that I'm trying to inherit
from.
The code for the base class is:

template <class T>
class BaseClass {
public:
BaseClass();
~BaseClass();

UINT getSize();
BOOL exists(UINT pos);
VOID swap(UINT nFirst, UINT nSecond);
VOID remove(UINT pos);

protected:
T* _head;
UINT _size;

virtual T* getNode(UINT pos);
};

I then inherit this class:

class AnotherClass: public BaseClass<VFS> {
public:
AnotherClass(); // Initializes _head and _size to 0
~AnotherClass(); // Removes all nodes by using BaseNode's remove
function
// Other methods that inserts nodes and sets the values of the
members in the VFS struct
};

Other methods in AnotherClass is for example adding a new node, setting
values of members in the VFS struct etc.

These classes are declared in a header file and defined in a cpp file.
The problem arises when compiling, when I get 4 linker errors of the
type:
3 x error LNK2019: unresolved external symbol
1 x error LNK2001: unresolved external symbol

If anyone knows what the problem could be I would be very pleased.
Thansk in advance.

Apr 8 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Well, put the definitions of the BaseClass members back to the header.

Regards,
Ben
Apr 8 '06 #2

P: n/a
Wow, that did it! But I would prefer to have the definitions in another
file than the header, just to make things look cleaner by just having
declarations of classes in the header, and then define them elsewhere.
Is that possible?

Apr 8 '06 #3

P: n/a
Murkland <mu******@gmail.com> wrote:
Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?


If your compiler supports the 'export' keyword for templates, yes.
But I highly doubt that it does, so practically, the answer is no.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 8 '06 #4

P: n/a
Jakob Bieling wrote:
Murkland <mu******@gmail.com> wrote:

Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?

If your compiler supports the 'export' keyword for templates, yes.
But I highly doubt that it does, so practically, the answer is no.

Or supports finding definitions in another file. That would be in your
documentation.

--
Ian Collins.
Apr 8 '06 #5

P: n/a
Ian Collins <ia******@hotmail.com> wrote:
Jakob Bieling wrote:
Murkland <mu******@gmail.com> wrote:
Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?
If your compiler supports the 'export' keyword for templates,

Or supports finding definitions in another file.


This is what 'export' is for .. ? In this particular case, at least.
--
jb

(reply address in rot13, unscramble first)
Apr 8 '06 #6

P: n/a
Murkland <mu******@gmail.com> wrote:
Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?


Another idea that came to mind: put the class definition in one
header file and the member function definitions in another. So you have:

--- BaseClass.h ---
template <class T>
class BaseClass {
public:
BaseClass();
~BaseClass();

UINT getSize();
BOOL exists(UINT pos);
VOID swap(UINT nFirst, UINT nSecond);
VOID remove(UINT pos);

protected:
T* _head;
UINT _size;

virtual T* getNode(UINT pos);
};

#include "BaseClass_impl.h"
--- end BaseClass.h ---

--- BaseClass_impl.h ---
template <typename T>
BaseClass <T>::BaseClass () {}

// ...
--- end BaseClass_impl.h ---

and the implementation is nicely seperated.

hth
--
jb

(reply address in rot13, unscramble first)
Apr 8 '06 #7

P: n/a
Thanks for all the answers. My problem is solved.

Apr 8 '06 #8

P: n/a
Jakob Bieling wrote:
Ian Collins <ia******@hotmail.com> wrote:

Jakob Bieling wrote:


Murkland <mu******@gmail.com> wrote:
Wow, that did it! But I would prefer to have the definitions in
another file than the header, just to make things look cleaner by
just having declarations of classes in the header, and then define
them elsewhere. Is that possible?
If your compiler supports the 'export' keyword for templates,


Or supports finding definitions in another file.

This is what 'export' is for .. ? In this particular case, at least.


No, some compilers (SunCC and (older?) VC++) can find template
definitions in another file. If a template is declared in foo.h, by
default the compiler with look for the definition in foo.cc/cxx/CC in
the same directory.

This is different behaviour from export, the compiler searches for the
definition each time it instantiates the template. But it still gives
you some of the advantages of having a separate definition file.

--
Ian Collins.
Apr 8 '06 #9

P: n/a
Ian Collins <ia******@hotmail.com> wrote:
Jakob Bieling wrote:
Ian Collins <ia******@hotmail.com> wrote:
Jakob Bieling wrote: Murkland <mu******@gmail.com> wrote: Wow, that did it! But I would prefer to have the definitions in
> another file than the header, just to make things look cleaner by
> just having declarations of classes in the header, and then define
> them elsewhere. Is that possible? If your compiler supports the 'export' keyword for templates, Or supports finding definitions in another file.
This is what 'export' is for .. ? In this particular case, at
least.

No, some compilers (SunCC and (older?) VC++) can find template
definitions in another file. If a template is declared in foo.h, by
default the compiler with look for the definition in foo.cc/cxx/CC in
the same directory.

This is different behaviour from export, the compiler searches for the
definition each time it instantiates the template. But it still gives
you some of the advantages of having a separate definition file.


Oh, I did not know that, thanks. I assume this non-Standard tho?
--
jb

(reply address in rot13, unscramble first)
Apr 9 '06 #10

P: n/a
Jakob Bieling wrote:
> If your compiler supports the 'export' keyword for templates,
Or supports finding definitions in another file.
This is what 'export' is for .. ? In this particular case, at
least.


No, some compilers (SunCC and (older?) VC++) can find template
definitions in another file. If a template is declared in foo.h, by
default the compiler with look for the definition in foo.cc/cxx/CC in
the same directory.

This is different behaviour from export, the compiler searches for the
definition each time it instantiates the template. But it still gives
you some of the advantages of having a separate definition file.

Oh, I did not know that, thanks. I assume this non-Standard tho?


I guess so, but then again, so is including definition files in headers!
How templates are compiled is an implementation specific thing.

But it is a much cleaner way of working and can improve build times.

--
Ian Collins.
Apr 9 '06 #11

P: n/a
Ian Collins wrote:
[snip]
No, some compilers (SunCC and (older?) VC++) can find template
definitions in another file. If a template is declared in foo.h, by
default the compiler with look for the definition in foo.cc/cxx/CC in
the same directory.

This is different behaviour from export, the compiler searches for the
definition each time it instantiates the template. But it still gives
you some of the advantages of having a separate definition file.

Oh, I did not know that, thanks. I assume this non-Standard tho?


I guess so, but then again, so is including definition files in headers!


Huh? Do you have chapter and verse for this one? If I am not mistaken, the
standard does not even know about the difference customarily made between
header files and implementation files.
How templates are compiled is an implementation specific thing.


But for the compiler to dig into a file that is not found via some include
directive is not implementation specific: it's non-standard. (Maybe, I am
not sure, one could argue it is an extension.)
Best

Kai-Uwe Bux

Apr 9 '06 #12

P: n/a
Kai-Uwe Bux wrote:
Ian Collins wrote:
[snip]
No, some compilers (SunCC and (older?) VC++) can find template
definitions in another file. If a template is declared in foo.h, by
default the compiler with look for the definition in foo.cc/cxx/CC in
the same directory.

This is different behaviour from export, the compiler searches for the
definition each time it instantiates the template. But it still gives
you some of the advantages of having a separate definition file.
Oh, I did not know that, thanks. I assume this non-Standard tho?
I guess so, but then again, so is including definition files in headers!

Huh? Do you have chapter and verse for this one? If I am not mistaken, the
standard does not even know about the difference customarily made between
header files and implementation files.

I don't need it, as you say, the standard says nothing about header
files and implementation files.
How templates are compiled is an implementation specific thing.


But for the compiler to dig into a file that is not found via some include
directive is not implementation specific: it's non-standard. (Maybe, I am
not sure, one could argue it is an extension.)

True.

--
Ian Collins.
Apr 9 '06 #13

P: n/a
Ian Collins wrote:
Kai-Uwe Bux wrote:
Ian Collins wrote:
[snip]
>No, some compilers (SunCC and (older?) VC++) can find template
>definitions in another file. If a template is declared in foo.h, by
>default the compiler with look for the definition in foo.cc/cxx/CC in
>the same directory.
>
>This is different behaviour from export, the compiler searches for the
>definition each time it instantiates the template. But it still gives
>you some of the advantages of having a separate definition file.
Oh, I did not know that, thanks. I assume this non-Standard tho?

I guess so, but then again, so is including definition files in headers!

Huh? Do you have chapter and verse for this one? If I am not mistaken,
the standard does not even know about the difference customarily made
between header files and implementation files.

I don't need it, as you say, the standard says nothing about header
files and implementation files.


Then, maybe I misunderstood. What did you mean by "including definition
files in headers is non-standard"? With regard to templates, including
definitions in header files appears to be widely used and blessed by the
standard in that every conforming implementation will support this
technique.
Best

Kai-Uwe Bux
Apr 9 '06 #14

P: n/a
Kai-Uwe Bux wrote:
Ian Collins wrote:

Kai-Uwe Bux wrote:
Ian Collins wrote:
[snip]
>>No, some compilers (SunCC and (older?) VC++) can find template
>>definitions in another file. If a template is declared in foo.h, by
>>default the compiler with look for the definition in foo.cc/cxx/CC in
>>the same directory.
>>
>>This is different behaviour from export, the compiler searches for the
>>definition each time it instantiates the template. But it still gives
>>you some of the advantages of having a separate definition file.
>
>
> Oh, I did not know that, thanks. I assume this non-Standard tho?

I guess so, but then again, so is including definition files in headers!
Huh? Do you have chapter and verse for this one? If I am not mistaken,
the standard does not even know about the difference customarily made
between header files and implementation files.


I don't need it, as you say, the standard says nothing about header
files and implementation files.

Then, maybe I misunderstood. What did you mean by "including definition
files in headers is non-standard"? With regard to templates, including
definitions in header files appears to be widely used and blessed by the
standard in that every conforming implementation will support this
technique.

OK, I surrender!

All I was saying is that there isn't a standard way (as in a process
laid down in the standard) of compiling templates. I just don't like
the (in my opinion) messy kludge of including the definition file in the
header. More of an aesthetic objection than anything else.

--
Ian Collins.
Apr 9 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.