Mail Archives: djgpp/1998/09/25/09:42:04
Hi Mr. Chen!
I guess I've already had the same problem in the past.
I don't know exactly why, but templates are not compiled in an
usual way. It seems to me that every time you make a "templatization", I
mean, when you try
SimpleArray<float> saf;
SimpleArray<int> sai;
SimpleArray<double> sad;
the compiler creates code for each "new" data type. It creates a code for
"SimpleArray<float>", "SimpleArray<int>" and so on ...
That means that when you make a template file, it must be
included, not compiled! Compilation takes place when you declare an
object template. I would do, for instance (details ommited):
// FILE: simarra.h
...
template <class T>
class SimpleArray
{
public:
SimpleArray( const int& );
...
};
In another file: simarra.hpp
// FILE: simarra.hpp
...
template <class T>
SimpleArray<T>::SimpleArray( const int& s_ ): ...do some stuff here
{
...
}
And finally ...
// FILE: main.cc
#include "simarra.hpp" <- ATENTION!!! I'm including the HPP!!!
main()
{
SimpleArray<float> s;
... and so on!
return 0;
}
OK???? Let me know if it works!
Renato F. CANTAO
State University at Campinas
Campinas - Brazil
On 25 Sep 1998, Shue-Cheng CHEN wrote:
> Hi!
>
> I made a main program "test.cc" as follows,
>
> #include <iostream.h>
> #include "simarra.h" // Header for SimpleArray<class T>
>
> int main()
> {
> SimpleArray<float> a;
> for (int i=1; i<=6; i++) {
> a[i] = i * 1.1;
> cout << a[i] << " ";
> }
> cout << endl;
> return 0;
> }
>
> It's strange that this main program only works when I placed all
> implementations of the class SimpleArray<T> in the same test.cc.
>
> When the implementations of the main program and the class were
> separated in two different files, e.g., test.cc and SimArra.cc,
> in the linking stage, I always got the messages
>
> 1.o: In function `main':
> 1.cc:1: undefined reference to `SimpleArray<float>::SimpleArray(void)'
> 1.cc:2: undefined reference to `SimpleArray<float>::SimpleArray(void)'
> 1.cc:6: undefined reference to `SimpleArray<float>::operator[](int)'
> 1.cc:7: undefined reference to `SimpleArray<float>::operator[](int)'
> 1.cc:11: undefined reference to `SimpleArray<float>::~SimpleArray(void)'
> 1.cc(.text+0xc9): undefined reference to
> `SimpleArray<float>::~SimpleArray(void)'
> 1.cc(.text+0xf3): undefined reference to
> `SimpleArray<float>::~SimpleArray(void)'
> 1.cc(.text+0x113): undefined reference to
> `SimpleArray<float>::~SimpleArray(void)'
>
> Why did it become undefined?
>
> Thank you in advance.
>
> Shue-Cheng Chen
>
- Raw text -