F
Fabio De Francesco
Hello.
I can't understand why I can't compile the following simple code, where
I think I have applied all the needed rules for templates that are
declared and defined in different files (*.h and *.cpp).
What amazes me is that I have already some code like this in another
project where I don't get errors, so I am pretty sure I am missing some
stupid thing.
// file templates.h
#ifndef _TMPLT_
#define _TMPLT_
#include <iostream>
template <typename T>
class my_class;
template <typename T>
std:stream& operator<<( std:stream& out, const my_class<T>& obj );
template <typename T>
class my_class
{
public:
my_class( const T& v );
private:
friend std:stream& operator<< <> (std:stream& out,const
my_class<T>& obj);
const T val;
};
#endif
// file templates.cpp
#include "templates.h"
template class my_class<int>;
template <typename T>
my_class<T>::my_class( const T& v = T() )
: val( v )
{ }
template <typename T>
inline std:stream& operator<<( std:stream &out, const my_class<T>
&obj )
{
return out << obj.val << '\n';
}
// file main.cpp
#include "templates.h"
int main()
{
my_class<int> h1(99);
std::cout << h1;
return 0;
}
I compile it with gcc-3.4.1:
myself@myhost /temporary # g++ -c templates.cpp -o templates.o -Wall
myself@myhost /temporary # g++ main.cpp templates.o -Wall
/tmp/ccx0vMtF.o(.text+0x144): In function `main':
: undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& operator<< <int>(std::basic_ostream<char,
std::char_traits<char> >&, my_class<int> const&)'
collect2: ld returned 1 exit status
Thanks in advance to everyone that points me to the errors in code.
Ciao,
Fabio De Francesco
I can't understand why I can't compile the following simple code, where
I think I have applied all the needed rules for templates that are
declared and defined in different files (*.h and *.cpp).
What amazes me is that I have already some code like this in another
project where I don't get errors, so I am pretty sure I am missing some
stupid thing.
// file templates.h
#ifndef _TMPLT_
#define _TMPLT_
#include <iostream>
template <typename T>
class my_class;
template <typename T>
std:stream& operator<<( std:stream& out, const my_class<T>& obj );
template <typename T>
class my_class
{
public:
my_class( const T& v );
private:
friend std:stream& operator<< <> (std:stream& out,const
my_class<T>& obj);
const T val;
};
#endif
// file templates.cpp
#include "templates.h"
template class my_class<int>;
template <typename T>
my_class<T>::my_class( const T& v = T() )
: val( v )
{ }
template <typename T>
inline std:stream& operator<<( std:stream &out, const my_class<T>
&obj )
{
return out << obj.val << '\n';
}
// file main.cpp
#include "templates.h"
int main()
{
my_class<int> h1(99);
std::cout << h1;
return 0;
}
I compile it with gcc-3.4.1:
myself@myhost /temporary # g++ -c templates.cpp -o templates.o -Wall
myself@myhost /temporary # g++ main.cpp templates.o -Wall
/tmp/ccx0vMtF.o(.text+0x144): In function `main':
: undefined reference to `std::basic_ostream<char,
std::char_traits<char> >& operator<< <int>(std::basic_ostream<char,
std::char_traits<char> >&, my_class<int> const&)'
collect2: ld returned 1 exit status
Thanks in advance to everyone that points me to the errors in code.
Ciao,
Fabio De Francesco