OVH Cloud OVH Cloud

template debutant

1 réponse
Avatar
docCarcass
Je suis debutant en c++ et je rencontre un probleme sur
un exemple simplisime, concernant une declaration de fonction friend.
Voila la classe, si qqun veut bien me debloquer, je cherche aussi ...

#ifndef _VECTEUR_HPP_
#define _VECTEUR_HPP_

#include <cstring>
#include <cassert>

#include <iostream>

template<class T> class Vector
{
friend std::ostream &operator<<(std::ostream &f, Vector &v);

private:
unsigned int n;
T *t;
public:
Vector(unsigned int n):n(n),t(new T[n]){assert(t);}

Vector(const Vector &v):n(v.n),t(new T[n])
{
operator=(v);
}

Vector &operator=(const Vector &v)
{
assert(n==v.n);
memcpy(t,v.t,n*sizeof(T));
return *this;
}

T &operator[](const unsigned int i) const
{
assert(0<=i && i<n);
return t[i];
}

~Vector(){delete [] t;}

};

template<class T>
std::ostream &operator<<(std::ostream &f, Vector<T> &v)
{
f <<"["; for(unsigned int i=0;i<v.n;i++) f << v.t[i] << " ;"; f << "]";
return f;
}

#endif


--

int main(){int j=1234,putchar();char t[]=":@abcdefghij-lmnopqrstuv"
"wxyz.\n",*i="@jq:.pn.q:ibf.gd\noz.dn@ew\nlwh-i",*strchr();while(*i)
{j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);}return 0;}

1 réponse

Avatar
docCarcass
Desole pour le derangement, j'ai trouve entre temps ...
C pas simple le c++ .

template<class T> class Vector
{
friend std::ostream &operator<< <T>(std::ostream &f, Vector<T> &v);

...

--

int main(){int j34,putchar();char t[]=":@abcdefghij-lmnopqrstuv"
"wxyz.n",*i="@jq:.pn.q:",*strchr();while(*i)
{j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);}return 0;}