help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

template instantiation g++ 4.0x


From: Tom Hilinski
Subject: template instantiation g++ 4.0x
Date: Mon, 20 Mar 2006 20:01:03 -0700
User-agent: Thunderbird 1.5 (X11/20060111)

I'm unable to get template code that builds with g++ 3.4.x to link
successfully in g++ 4.0.x   I'd appreciate any suggestions.
A search in bug reports in for 4.0 didn't show me anything
that seemed to fit this.

Code for a simplified situation is presented below.
In the link stage, g++ says:

tom@linux:~/tests> g++ ATemplateMain.cpp ATemplate.cpp
/tmp/cc4mEu4b.o: In function `main':
ATemplateMain.cpp:(.text+0x20): undefined reference to
`ATemplate<int>::GetValue() const'
ATemplateMain.cpp:(.text+0x51): undefined reference to
`ATemplate<float>::GetValue() const'
collect2: ld returned 1 exit status

Here is the code:

// ATemplate.h

template <class T> class ATemplate
{
        T const n;
  public:
        ATemplate () : n (1) { }
        T GetValue () const;
};

// ATemplateInst.h

#include "ATemplate.h"

template class ATemplate<int>;
template class ATemplate<float>;

// ATemplate.cpp

#include "ATemplateInst.h"

template <class T>
T ATemplate<T>::GetValue () const
{
        return n;
}

// ATemplateMain.cpp

#include <iostream>
#include "ATemplateInst.h"

int main ()
{
        ATemplate<int> intTemplate;
        ATemplate<float> floatTemplate;

        std::cout << "ATemplate<int>::n =   " << intTemplate.GetValue()
<< std::endl;
        std::cout << "ATemplate<float>::n = " <<
floatTemplate.GetValue() << std::endl;

        return 0;
}


Thanks much... Tom


reply via email to

[Prev in Thread] Current Thread [Next in Thread]