Взываю ващей помощи, скока мучал дядющку google так они ничего и не выдал.
Код:
class LIST
{
...
friend ostream& operator<< (ostream&, const LIST<T>&);
...
}
...
template <typename T>
ostream& operator<< (ostream& p, const LIST<T>& L)
{
...
}
...
void TypeInt (void)
{
...
LIST <int> L;
...
cout<<"Current list:\t"<<L;
...
}
////////////EOF//////////////
:!make |& tee /tmp/v537941/6
c++ main.cpp -g3 -DFreeBSD -DDEBUG
main.cpp:48: warning: friend declaration 'std::ostream& operator<<(std::ostream&, DATA<T>&)' declares a non-template function
main.cpp:48: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -
Wno-non-template-friend disables this warning
main.cpp:70: warning: friend declaration 'std::ostream& operator<<(std::ostream&, const LIST<T>&)' declares a non-template function
/var/tmp//ccKC61Po.o(.text+0x5ba): In function `TypeInt()':
/home/asm/Mtuci/cpp/OOP/Lab5/main.cpp:367: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, LIST<int> const&)'
/var/tmp//ccKC61Po.o(.text+0xa04): In function `TypePoezd()':
/home/asm/Mtuci/cpp/OOP/Lab5/main.cpp:409: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, LIST<POEZD> const&)'
collect2: ld returned 1 exit status
*** Error code 1
В коде не видно, что класс LIST шаблонный.
Если это так, то код должен выглядеть примерно так:
template< typename T >
class LIST
{
...
template< typename T2 >
friend ostream& operator<< (ostream&, const LIST<T2>&);
...
};
Остальное вроде правильно.
Не забывайте, что функция-друг не является членом класса,
и на неё не распространяется "шаблонность" класса.
Вот код, который компилируется и работает правильно:
#include <iostream>
using namespace std;
template< typename T >
class MyCl
{
public:
template< typename Z >
friend ostream& operator<<(ostream &stream, const MyCl<Z> &cl);
};
template< typename T >
ostream& operator<<(ostream &stream, const MyCl<T> &cl)
{
stream << "hi\n";
return stream;
}
int main( )
{
MyCl<int> c;
std::cout << c;
}
Переделал на
template< typename T2 >
friend ostream& operator<< (ostream &stream, const MyCl<T> &cl);
теперь почему-то копмилируется, так и не понял, что это значит но за то это работает :-)
СПАСИБО ВАМ! что бы я без вас делал!!!