LINUX.ORG.RU

c++ templates


0

2
#include <iostream>
using namespace std;

template <class T>
class array &#123;
public:
&nbsp; &nbsp;array(int s=10);
&nbsp; &nbsp;array(const array &rhs);
&nbsp; &nbsp;~array() &#123; delete &#91;&#93; type; --num;&#125;
&nbsp; &nbsp;
&nbsp; &nbsp;array& operator=(const array&);
&nbsp; &nbsp;T& operator&#91;&#93;(int offset) &#123;return type&#91;offset&#93;;&#125;
&nbsp; &nbsp;const T& operator&#91;&#93;(int offset) const
&nbsp; &nbsp;&nbsp; &nbsp;&#123;return type&#91;offset&#93;;&#125;&nbsp; &nbsp;

&nbsp; &nbsp;int itssize() &#123; return size;&#125;
&nbsp; &nbsp;template <T>&nbsp; &nbsp;
&nbsp; &nbsp;friend ostream& operator<<(ostream&,array<T>&);
&nbsp; &nbsp;//template <T>
&nbsp; &nbsp;
&nbsp; &nbsp;static int getnum() &#123;return num;&#125;
private:
&nbsp; &nbsp;T *type;
&nbsp; &nbsp;int size;
&nbsp; &nbsp;static int num;
&#125;;
template <class T>
int array<T>::num = 0;

template <class T>
ostream & operator<<(ostream& output,array<T>& arrg) &#123;
&nbsp; &nbsp;for(int i=0;i<arrg.itssize();i++)
&nbsp; &nbsp;&nbsp; &nbsp;output<<arrg&#91;i&#93;<<endl; 
&nbsp; &nbsp;return output;
&#125;

template <class T>
array<T>::array(int s):size(s) &#123;
&nbsp; &nbsp;type = new T&#91;size&#93;;
&nbsp; &nbsp;for(int i=0;i<size;i++)
&nbsp; &nbsp;&nbsp; &nbsp;type&#91;i&#93; = (T)0;
&nbsp; &nbsp;num++;
&#125;

template <class T>
array<T>::array(const array & rhs) &#123;
&nbsp; &nbsp;int s = rhs.itssize();
&nbsp; &nbsp;type = new T&#91;s&#93;;
&nbsp; &nbsp;for(int i =0 ;i<s;i++)
&nbsp; &nbsp;&nbsp; &nbsp;type&#91;i&#93; = rhs&#91;i&#93;;
&nbsp; &nbsp;num++;
&#125;

template <class T>
array<T> & array<T>::operator=(const array &rhs) &#123;
&nbsp; &nbsp;if(this == &rhs)
&nbsp; &nbsp;&nbsp; &nbsp;return *this;
&nbsp; &nbsp;delete &#91;&#93; type;
&nbsp; &nbsp;int s = rhs.itssize();
&nbsp; &nbsp;type = new T&#91;s&#93;;
&nbsp; &nbsp;for(int i=0;i<size;i++)
&nbsp; &nbsp;&nbsp; &nbsp;type&#91;i&#93; = rhs&#91;i&#93;;
&nbsp; &nbsp;return *this;
&#125;

int main()
&#123;
&nbsp; &nbsp;cout<<array<int>::getnum()<<endl;
&nbsp; &nbsp;cout<<array<float>::getnum()<<endl;
&nbsp; &nbsp;
&nbsp; &nbsp;array<int> that;
&nbsp; &nbsp;array<float> athat;

&nbsp; &nbsp;cout<<that.getnum()<<endl;
&nbsp; &nbsp;cout<<athat.getnum()<<endl;
&nbsp; &nbsp;
&nbsp; &nbsp;array<int>* p=new array<int>;
&nbsp; &nbsp;
&nbsp; &nbsp;cout<<that.getnum()<<endl;
&nbsp; &nbsp;cout<<athat.getnum()<<endl;
&nbsp; &nbsp;
&nbsp; &nbsp;delete p;

&nbsp; &nbsp;cout<<that.getnum()<<endl;
&nbsp; &nbsp;cout<<athat.getnum()<<endl;

&nbsp; &nbsp;return 0;
&#125;
main.cpp: In instantiation of ‘array<float>’:
main.cpp:93:   instantiated from here
main.cpp:34: error: ‘float’ is not a valid type for a template constant parameter

вот это

   template <T>   
   friend ostream& operator<<(ostream&,array<T>&);
заменить на
   template <class U>
   friend ostream& operator<<(ostream&,array<U>&);

anonymous ()

main.cpp:34: error: ‘float’ is not a valid type for a template constant parameter

Ну написно же!

float/double не может быть параметром шаблона по стандарту языка. Интегральные типы допускатся.

Pavval ★★★★★ ()

Торвальдс плакал.

bk_ ★★ ()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.