I was trying out the following piece of code:
GeneralTemplate.h
#ifndef _GENERATEMPLATE_H_
#define _GENERATEMPLATE_H_
#include <iostream>
template <class T>
class GeneralTemplate
{
public:
GeneralTemplate();
GeneralTemplate(const GeneralTemplate &g);
~GeneralTemplate();
GeneralTemplate& operator= (GeneralTemplate const& g);
template <class M>
void arbitraryFunction(const M &m);
};
#endif
main.cpp
#include "GeneralTemplate.h"
#include <iostream>
int main()
{
GeneralTemplate<int> gInt;
gInt.arbitraryFunction(2.3);
return 0;
}
Note that I don't have any implementation for the member functions of the class template. But that is not the problem. I know how to do that! If I try to compile main.cpp, I should get a linking error and that's what I get. The question is why is it trying to find the destructor twice (last two lines of error below).
$g++ main.cpp
/tmp/cckrdPCs.o: In function `main':
main.cpp:(.text+0x13): undefined reference to `GeneralTemplate<int>::GeneralTemplate()'
main.cpp:(.text+0x34): undefined reference to `void GeneralTemplate<int>::arbitraryFunction<double>(double const&)'
main.cpp:(.text+0x45): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
main.cpp:(.text+0x61): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
collect2: ld returned 1 exit status
_GENERATEMPLATE_H_
is a reserved identifier. You should also check with GCC to see what it did to the code so you can match up the given locations. – Davide<codereview>
It seems to me that#include <iostream>
is not needed in your header.</codereview>
– AlmatademaarbitraryFunction(2.3);
? – Korenblatthrow()
(ornoexcept
in C++11), and still probably have the same behaviour, i.e. no more than 1 dtor call. – Korenblat