Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > friend и template в классе


Автор: ano360 24.7.2006, 00:43
как подключить friend функцию в template класс.
Код

#include <iostream>
#include <cstdlib>

template <class Data> class List{
protected:

       List *begin;
       List *end;
       List *next;
       Data data;
public:
        /*
       прочие вункции, в том числе и конструктор
      .............
       */
       /*Строка 22*/ friend List<Data> &operator+(Data i,List<Data> &li2);
       /*
       другие прочие функции
      .............
       */
};
/*
прочие функции
*/
template <class Data>
List<Data> &operator+(Data i,List<Data> &li2){
      List<Data> *newl;
      newl=new List<Data>;
      newl->data=i;
      if (li2.begin)newl->next=li2.begin;
      li2.begin=newl;
      if(!li2.end)li2.end=newl; 
      return li2;
}

/*
прочие
*/
int main()
{
List<int> li1;
/*строка 172*/9+li1;
system("PAUSE");
      return 0;
}

Если запускать без строки 172, компилятор страшно ругается, но запускается, а если с ней то вылетает тысяча и одно ошибка, если саму функцию вырезать /* */, то программа тоже прекрасно работает.
Ругательства:Все в строке 22 
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: friend declaration `class List<Data> & operator +(Data, List<Data> &)'
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: declares a non-template function
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: (if this is not what you intended, make sure
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: the function template has already been declared,
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: and add <> after the function name here)
22 c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp warning: -Wno-non-template-friend disables this warning.

1001 ошибка:
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning: friend declaration `class List<Data> & operator +(Data, List<Data> &)'
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning:   declares a non-template function
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning:   (if this is not what you intended, make sure
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning:   the function template has already been declared,
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning:   and add <> after the function name here)
c:\docume~1\9335~1\c316~1\c\nugnics\ideala~2.cpp:22: warning:   -Wno-non-template-friend disables this warning.
C:\DOCUME~1\9335~1\LOCALS~1\Temp\ccE3baaa.o(.text+0x87):ideala~2.cpp: undefined reference to `operator+(int, List<int> &)'
Только это уже в строке компилер и линкер оутпут, редактор дев цпп 4

В самоучителе на тему создания фриенд шаблонов функций ничего не сказано, я уж искать где не знаю smile 
может вы подскажите.


  

Автор: zkv 24.7.2006, 02:48
когда то что то объясняли, вроде так:
Код

#include <iostream>
template <class Data> 
class List
{
    Data a;
public:
    List()
    {
        a=static_cast<Data>(1);
    }
    template <class Data> //!!!!!!!!!!!!!!!!!!!!!!!!!
    friend List<Data> & operator+(Data , List<Data> &);
};
template <class Data>
List<Data> & operator+(Data i, List<Data> &cl)
{
    std::cout<<cl.a+i;
    return cl;
}
int main()
{
    List<int> a;
    9+a;
}

Цитата

Differences from Other ImplementationsSee Also
Templates
Microsoft Specific

The following list shows some differences between Microsoft C++ and other compilers. Note that this list will change in future versions of the compiler. 

Friend functions must be declared before they are used in a templated class. You cannot have a friend function defined within a class definition. This is because the friend function could be a templated function, which would cause an illegal nested template definition. 
END Microsoft Specific
 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)