* Justin Findlay [Wed,  3 Mar 2004 at 16:05 -0700]
<quote>
> On Wed, Mar 03, 2004 at 03:45:05PM -0700, Von Fugal wrote:
> > I've plunged myself into the wonderful world of templates in c++. It was
> > fairly easy to figure out the syntax and everything for creating templates,
> > and my object files all compiled. However, for some reason whenever I try to
> > link the program, all I get is 'undefined reference's to function calls
> > belonging to the templates. Could anyone be so kind as to enlighten me to
> > possible causes and solutions to this problem? I've strained my brain and
> > the chapter on templates in my c++ book to no avail.
> 
> Could you send an example or your whole source tree if you don't have time to 
> construct an example to the list or me personally?  Are you using gcc?  Which 
> version?

Here's a really simple example with the same problem I'm having. I must be
doing something wrong, and I would guess it's probably in the bag.cpp file.

using g++, 
'g++ -v' says:
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man 
--infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking 
--with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
bag: bag.o main.o
        g++ -o bag $^
#include "bag.h"

template <class T> Bag<T>::Bag() {}
template <class T> Bag<T>::Bag(const T & val) : contents(val) {}

template <class T> T Bag<T>::getContents() {
        return contents;
}
#ifndef BAG_H
#define BAG_H

template <class T> class Bag {
        private:
                T contents;
        public:
                Bag();
                Bag(const T &);

                T getContents();
};

#endif
#include "bag.h"
#include <string>
#include <iostream>

using namespace std;

int main() {
        Bag<char> b('c');

        cout << b.getContents() << endl;
}

Attachment: pgp00000.pgp
Description: PGP signature

____________________
BYU Unix Users Group 
http://uug.byu.edu/ 
___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to