473,320 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template class inheritance problem

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
14 4406
Well, put the definitions of the BaseClass members back to the header.

Regards,
Ben
Apr 8 '06 #2
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
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
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
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
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
Thanks for all the answers. My problem is solved.

Apr 8 '06 #8
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Gandu | last post by:
Could some C++ guru please help me. I have a template based general linked list class that I want to inherit publicly to create a queue class. In the case of non-template inheritance, I can use:...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: Pallav singh | last post by:
I am Facing Problem while creating object of Diamond Ring problem solving using Template Kindly let me known where i am committing ERROR Thanks Pallav #include<iostream.h> #include<string.h>
4
by: danilo.turina | last post by:
Hi all, today I encountered a problem that I'm only able to solve by using reinterpret_cast (and I'd like to avoid it). This was my code until yesterday (omitting includes, "using namespace"...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.