Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

Temlate member of class, LNK2028 and Static Library.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
pkoniusz@gmail.com - 21 Mar 2007 16:23 GMT
Hello everyone.

The problem may be obvious, though I'm a bit puzzled by the error
LNK2028 when attempting to utilize my static library. The all methods
of the class defined within that library do not pose any troubles, but
template based methods.

Let's say this is a header:
class LooseFunctions
{
    private:
    public:
        // <== probe, if a given file can be opened for reading
        bool CanOpenFileByName(const CHAR *psFileNameC);

        template <class ElType>
            ElType** AllocateBuffers(const unsigned nRequiredBuffSizeC,
            const unsigned nRequiredElSizeC);
}

There is appropriate definition of body content in a cpp file. The
question is why the LNK2028 happend with resect to the above-declared
template method? When I copy and paste the body of that method to the
h file, all works fine. All remining methods (utilized in project)
that are not template based are perfectly happy to be infused with
their body in the cpp. WHat's the matter?

Cheers,
Peter.
Ben Voigt - 21 Mar 2007 17:03 GMT
> Hello everyone.
>
[quoted text clipped - 9 lines]
> that are not template based are perfectly happy to be infused with
> their body in the cpp. WHat's the matter?

Templates aren't compiled until the parameters are known, so the compiler
needs source code (not just a static library) to make new template
instances.  So either put the template source in your header file, or
pre-instantiate all the different combinations you might want in the
library.
pkoniusz@gmail.com - 22 Mar 2007 10:11 GMT
> > The problem may be obvious, though I'm a bit puzzled by the error
> > LNK2028 when attempting to utilize my static library. The all methods
[quoted text clipped - 7 lines]
> > that are not template based are perfectly happy to be infused with
> > their body in the cpp. WHat's the matter?

> Templates aren't compiled until the parameters are known, so the compiler
> needs source code (not just a static library) to make new template
> instances.  So either put the template source in your header file, or
> pre-instantiate all the different combinations you might want in the
> library.

Hiah. I've assumed that this is the problem, though one more question.
How to preinstantiate a class along with these particular template
based methods? Would u give me example for the AllocateBuffers method
and, let's say, substitute the int type for ElType?

Cheers,
Peter.
Ben Voigt - 22 Mar 2007 19:55 GMT
> Hiah. I've assumed that this is the problem, though one more question.
> How to preinstantiate a class along with these particular template
> based methods? Would u give me example for the AllocateBuffers method
> and, let's say, substitute the int type for ElType?

http://www.comeaucomputing.com/techtalk/templates/#whylinkerror

I think it would be:

template <> int** AllocateBuffers<int>(const unsigned nRequiredBuffSizeC,
const unsigned nRequiredElSizeC);

> Cheers,
> Peter.
Tamas Demjen - 22 Mar 2007 23:07 GMT
> http://www.comeaucomputing.com/techtalk/templates/#whylinkerror
>
> I think it would be:
>
> template <> int** AllocateBuffers<int>(const unsigned nRequiredBuffSizeC,
> const unsigned nRequiredElSizeC);

Since it's a member function, you need to add "LooseFunctions::":

template int** LooseFunctions::AllocateBuffers<int>(const unsigned
nRequiredBuffSizeC, const unsigned nRequiredElSizeC);

Tom
Tom Widmer [VC++ MVP] - 23 Mar 2007 11:13 GMT
>> http://www.comeaucomputing.com/techtalk/templates/#whylinkerror
>>
[quoted text clipped - 7 lines]
> template int** LooseFunctions::AllocateBuffers<int>(const unsigned
> nRequiredBuffSizeC, const unsigned nRequiredElSizeC);

Just to clarify, that should be in the CPP file along with the
non-template function definitions.

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.