www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/15/20:16:53

Message-ID: <373D6221.405F04B5@geocities.com>
From: Andrew Geers <andrew_geers AT geocities DOT com>
X-Mailer: Mozilla 4.5 [en] (Win98; I)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Problem with Templates
Lines: 49
Date: Sat, 15 May 1999 12:03:22 GMT
NNTP-Posting-Host: 195.8.86.138
X-Complaints-To: abuse AT clara DOT net
X-Trace: nnrp2.clara.net 926769802 195.8.86.138 (Sat, 15 May 1999 13:03:22 BST)
NNTP-Posting-Date: Sat, 15 May 1999 13:03:22 BST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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

- Raw text -


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