www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/16/08:13:26

Message-ID: <373DEA8B.AEF818E9@enter.net>
Date: Sat, 15 May 1999 17:43:39 -0400
From: Sean <sproctor AT enter DOT net>
X-Mailer: Mozilla 4.51 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem with Templates
References: <373D6221 DOT 405F04B5 AT geocities DOT com>
NNTP-Posting-Host: 207.16.153.53
X-Original-NNTP-Posting-Host: 207.16.153.53
X-Trace: 15 May 1999 17:43:09 -0400, 207.16.153.53
Organization: Enter.Net
Lines: 57
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Andrew Geers wrote:
> 
> Templates appear to work perfectly in DJGPP at first glance, but if when
> I tried to separate my class definition into a separate CPP file and add
> it to a project file I start getting problems:
> 
> I've put the following code in 'header.h' and #included it in all of my
> CPP files in the project:
> 
> template<class T>
> class TEST
> {
>     private:
>         T *testpointer;
>    public:
>         void testmethod(T *point);
> }
> 
> Then in testdef.cpp (which is included in the project) I have the
> following definition:
> 
> template<class T>
> void TEST<T>::testmethod(T *point)
> {
>     testpointer=point;
> }
> 
> However, when in main.cpp (which is also in the project) I try the
> following code the linker complains:
> 
> void main()
> {
>     TEST<int> testclass;
>     int a=5;
>     int *b=&a;
> 
>     testclass.testmethod(b);
> }
> 
> The linker says "main.cpp(..) Error: Undeclared reference to
> 'TEST<int>::testmethod(int *)'"
> 
> However, it works perfectly if I get rid of testdef.cpp and put the
> definition of testmethod in header.h or at the top of main.cpp, so it
> doesn't look like it's anything to do with my code, but rather something
> to do with the linker. Does anybody know if this is the case, and if so,
> what should be done?
> 
> Thanks in advance,
>     Andrew Geers

All templates are like that.  Generally, I think, it's preferred to
#include the cpp file at the end of the header file, but the cpp file
needs to be #include'd somewhere, not just in the project file.  Well,
maybe there's a compiler unlike that, but that's ANSI C++.

Sean

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019