delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/03/03/18:19:08

Message-ID: <38C03E09.E7E7A8AC@bluemartini.com>
From: Sergei Severin <sergei AT bluemartini DOT com>
X-Mailer: Mozilla 4.7 [en] (WinNT; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: building source with C++ templates
Lines: 61
Date: Fri, 03 Mar 2000 22:35:02 GMT
NNTP-Posting-Host: 209.157.149.218
X-Complaints-To: abuse AT flash DOT net
X-Trace: news.flash.net 952122902 209.157.149.218 (Fri, 03 Mar 2000 16:35:02 CST)
NNTP-Posting-Date: Fri, 03 Mar 2000 16:35:02 CST
Organization: FlashNet Communications, http://www.flash.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I am trying to figure out how to compile and link my program if I am
using C++ templates. Here is the simple example of what I am trying to
accomplish:

- I have 3 files
- File 1 :  test.h

#iclude <iostream>
template<typename T>
    class foo
   {
      public:
          foo(T t);
          T getValue();
      private:
          T val;
   };

- File 2 : test.cpp

#include "test.h"
template<typename T>
foo<T>::foo(T t) {
    val = t;
}

template<typename T>
T foo<T>::getValue()
{
    return val;
}

- File 3: main.cpp

#include "test.h"
void main () {
    foo<int>* f = new foo<int>(5);
    cout << f->getValue();
}

- When I try to build it like:  gxx -o test.exe *.cpp   I get
d:/djgpp/tmp\ccTABxia.o(.text+0x23):main.cpp: undefined reference to
`foo<int>::foo(int)'
d:/apps/djgpp/tmp\ccTABxia.o(.text+0x5f):main.cpp: undefined reference
to `foo<int>::getValue(void)'
collect2: ld returned 1 exit status

I manage to build everything fine only in two cases: 1. if don't use
templates at all or 2. If I use templates but keep declarations and
implementations in one .h file

In my project, the files are pretty large and I am required to separate
class declarations from the implementation of member functions. So if
anyone knows how to fix my problem, I would really appreciate your help
with this.

Thank you,
Sergei Severin



- Raw text -


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