M
mast2as
Hi guys, I think from what I found on the net that the code is correct
and there's no problem when I compile the files. The problems occurs
when I link them. I have a friend function which outputs points data to
the ostream. Here is the error message:
objs/generalpolygons.o(.text+0xf08): In function
`GeneralPolygons::readObjFile(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
core/generalpolygons.cpp:222: undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& operator<<
<float>(std::basic_ostream<char, std::char_traits<char> >&,
Point<float> const&)'
Here is the code:
In point.hpp
// foward declaration needed for the template friend functions
template<typename T> class Point;
// forward declarion of the Point's friend function
template<typename T> std:stream & operator<<( std:stream &os,
const Point<T> &p );
template<typename T>
class Point
{
public:
...
friend std:stream & operator<< <T>( std:stream &os, const
Point<T> &p );
};
in point.cpp
template<typename T>
std:stream & operator<<(std:stream &os, const Point<T> &p )
{
os << p.x << " " << p.y << " " << p.z;
return os;
}
how I use it in the code
std::cout << Point<float>( 0.0 ) << std::endl;
Thanks for your help, Mark
and there's no problem when I compile the files. The problems occurs
when I link them. I have a friend function which outputs points data to
the ostream. Here is the error message:
objs/generalpolygons.o(.text+0xf08): In function
`GeneralPolygons::readObjFile(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
core/generalpolygons.cpp:222: undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& operator<<
<float>(std::basic_ostream<char, std::char_traits<char> >&,
Point<float> const&)'
Here is the code:
In point.hpp
// foward declaration needed for the template friend functions
template<typename T> class Point;
// forward declarion of the Point's friend function
template<typename T> std:stream & operator<<( std:stream &os,
const Point<T> &p );
template<typename T>
class Point
{
public:
...
friend std:stream & operator<< <T>( std:stream &os, const
Point<T> &p );
};
in point.cpp
template<typename T>
std:stream & operator<<(std:stream &os, const Point<T> &p )
{
os << p.x << " " << p.y << " " << p.z;
return os;
}
how I use it in the code
std::cout << Point<float>( 0.0 ) << std::endl;
Thanks for your help, Mark