This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

templates undefined refrence problem


I can't seem to be able to instantiate template classes of different
types.  I am using a template class that contains another template class
with seperate definitions and declarations in headers and implementation
files for each class.  When I compile, I consistantly get compiler
errors "undefined refrence" even though they compile to .o files
successfully.  The problem comes when it tries to link.

It appears to me that the compiler is not automaticly producing the
specializations that I am instantiating in my main function.  I have
tried differennt method to try and make this happen, by using -frepo,
bare compiler options, compiling all files at once but to no avail.
I was unable to find any compiler options to turn on template based on
the instantitation.  Using GCC 3.0.3 and 2.95.3.

What am I doing wrong?


A.hh
#ifndef T_HH
#define T_HH
template <class T> class A
{
    public:
        A();
};
#endif

A.cc
#include "A.hh"
template <class T> A<T>::A()
{  //...
}

B.hh
#include "A.hh"
#ifndef B_HH
#define B_HH
template <class T> class B
{
    public:
        B();
    private:
        A<T> a;
};

B.cc
#include "B.hh"
template <class T> B<T>::B()
{ //...
}

main.cc
#include "B.hh"
int main()
{
    A<int> ai;
    A<double> ad;
    return 0;
}

Makefile
CXX = g++
CXXFLAGS = -g -Wall -frepo
objects = A.o B.o main.o
all: $(objects)
 $(CXX) $(CXXFLAGS) -o $(program) $(objects)
A.o: A.cc A.hh
B.o: B.cc B.hh A.hh
main.o: main.cc B.hh

$ make
g++ -g -Wall -frepo   -c -o A.o A.cc
g++ -g -Wall -frepo   -c -o B.o B.cc
g++ -g -Wall -frepo   -c -o main.o main.cc
main.cc: In function `int main()':
main.cc:4: warning: unused variable `A<int> ai'
main.cc:5: warning: unused variable `A<double> ad'
g++ -g -Wall -frepo -o  A.o B.o main.o
main.o: In function `main':
main.cc:4: undefined reference to `A<int>::A()'
main.cc:5: undefined reference to `A<double>::A()'
collect2: ld returned 1 exit status
make: *** [all] Error 1





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]