G
Gilles DRIDI
Hello,
I've written a small data matrix manipulation (I want to use vector even if
transformation is slower !
because Vector is a subtype of matrix really)
with template. But it won't compile under G++ and give strange warning under
VS2003
This error with G++ is
for Vecteur<T, C> & Vecteur<T, C>:
perator = (Vecteur<T, C> &) give
"no match for 'operator =' in 'v = Vecteur<T, C>:
perator *(T)" the
affectation.
Under VS2003 it gives a extended warning only...
-------------------------
#include "vec.h"
#include <iostream>
#define DIM 2
using namespace std;
void rotate(int t[], long card) {
int last= t[card-1];
for(long i = card-1; i > 0; i--) t = t[i-1];
t[0]= last;
}
int main(int argc, char *argu[]) {
int ti[]= {1, 0};
Vecteur<int, DIM> i(ti);
rotate(ti, DIM);
Vecteur<int, DIM> j(ti);
cout<<i<<endl<<j<<endl;
Vecteur<int, DIM> v1, v;
cin>>v1;
/* == error here === */
v= (v1*3);
/* ============= */
cout<<v<<endl;
return 0;
}
-------------------------
#ifndef VECTEUR_H
#define VECTEUR_H
#include <iostream>
using namespace std;
template <class T, long Ci, long Cj>
class Matrix;
template <class T, long C>
/* a transposed vector normally yield a matrix : to do the beginning to
Matrix */
class Vector {
public:
T tab[C];
Vector(T t[]) { for(long i = 0; i < C; i++) tab = t; }
Vector() { for(long i = 0; i < C; i++) tab = 0; }
//template??? friend ostream & operator << (ostream &, Vector<T, C> &);
Vector<T, C> & operator = (Vector<T, C> &v) {
for(long i = 0; i < C; i++) tab = v;
return *this;
}
T operator [] (long i) { return tab; }
Vector(Vector<T, C> &v) { // copy constructor
for(long i = 0; i < C; i++) tab = v;
}
void i() { for(long i = 0; i < C; i++) if ( i == 0 ) tab = 1; else
tab = 0; }
void j() { for(long i = 0; i < C; i++) if ( i == 1 ) tab = 1; else
tab = 0; }
void k() { for(long i = 0; i < C; i++) if ( i == 2 ) tab = 1; else
tab = 0; }
Vector<T, C> operator * (T e);
};
template<class T, long C>
Vector<T, C> Vecteur<T, C>:
perator * (T e) {
Vector<T, C> v(*this);
for(long i = 0; i < C; i++) v.tab = tab * e;
return v;
}
template <class T, long Ci, long Cj> // line, column
class Matrix { // tableau de vecteurs sur plusieurs lignes (ou en colonne
trans())
public:
//friend class Vector;
Vector<T, Cj> mat[Ci];
Matrix(Vecteur<T, Cj> tabv[]) {
for(long i = 0; i < Ci; i++) mat = tabv;
}
Matrix() {
for(long i = 0; i < Ci; i++) {
Vector<T, Cj> v;
mat = v;
}
}
Matrix(Vector<T, Cj> v) {
mat[0] = v;
for(long i = 1; i < Ci; i++) {
Vector<T, Cj> v;
mat = v;
}
}
Matrix<T, Ci, Cj> & operator = (Matrix<T, Ci, Cj> &m) {
for(long i = 0; i < Ci; i++) mat = m;
return *this;
}
Matrix<T, Ci, Cj> & operator = (Vector<T, Cj> &v) { mat[0] = v; return
*this; }
Vector<T, Cj> & operator [] (long i) { return mat; }
Matrix(Matrix<T, Ci, Cj> &m) { // copy constructor
for(long i = 0; i < Ci; i++) mat = m;
}
void transpose(); //modify for moment, not exactly what we want
void identity(); // modify
};
template <class T, long Ci, long Cj>
void Matrix<T, Ci, Cj>::transpose() {
for(long i = 0; i < Ci; i++)
for(long j = i+1; j < Cj; j++) {
T elt= mat[j];
//printf("i%d, j%d : elt %d [][]%d\n", i,j, elt, mat[j]);
mat[j].tab = mat[j];
mat.tab[j] = elt;
}
}
template <class T, long Ci, long Cj>
void Matrix<T, Ci, Cj>::identity() {
for(long i = 0; i < Ci; i++) {
for(long j = 0; j < Cj; j++)
if (i == j) mat.tab[j] = 1; else mat.tab[j] = 0;
}
}
template <class T, long C>
ostream & operator << (ostream &o, Vector<T, C> &v) {
o<<"[";
for(long i = 0; i < C; i++) {
o<<v.tab;
if (i+1 < C) o<<"\t";
}
o<<"]";
return o;
}
template <class T, long Ci, long Cj>
ostream & operator << (ostream &o, Matrix<T, Ci, Cj> &m) {
o<<"[";
for(long i = 0; i < Ci; i++) {
if (i == 0) ; else o<<" ";
o<<m;
if (i+1 < Ci) o<<"\n";
}
o<<"]";
return o;
}
template <class T, long C>
istream & operator >> (istream &is, Vector<T, C> &v) {
char c;
is>>c;
if (c != '[') {
cerr<<"Enter Vector between brackets '[' "<<v;
exit(1);
}
for(long i = 0; i < C; i++) {
is>>v.tab;
}
is>>c;
if (c != ']') {
cerr<<"Enter Vector between brackets ']' "<<v;
exit(1);
}
return is;
}
#endif
-------------------------
vec.h is the definition classes' file
v2.cpp the small test program.
Thank you a lot, helping me.
Gilles DRIDI
http://cdridi.club.fr
I've written a small data matrix manipulation (I want to use vector even if
transformation is slower !
because Vector is a subtype of matrix really)
with template. But it won't compile under G++ and give strange warning under
VS2003
This error with G++ is
for Vecteur<T, C> & Vecteur<T, C>:
"no match for 'operator =' in 'v = Vecteur<T, C>:
affectation.
Under VS2003 it gives a extended warning only...
-------------------------
#include "vec.h"
#include <iostream>
#define DIM 2
using namespace std;
void rotate(int t[], long card) {
int last= t[card-1];
for(long i = card-1; i > 0; i--) t = t[i-1];
t[0]= last;
}
int main(int argc, char *argu[]) {
int ti[]= {1, 0};
Vecteur<int, DIM> i(ti);
rotate(ti, DIM);
Vecteur<int, DIM> j(ti);
cout<<i<<endl<<j<<endl;
Vecteur<int, DIM> v1, v;
cin>>v1;
/* == error here === */
v= (v1*3);
/* ============= */
cout<<v<<endl;
return 0;
}
-------------------------
#ifndef VECTEUR_H
#define VECTEUR_H
#include <iostream>
using namespace std;
template <class T, long Ci, long Cj>
class Matrix;
template <class T, long C>
/* a transposed vector normally yield a matrix : to do the beginning to
Matrix */
class Vector {
public:
T tab[C];
Vector(T t[]) { for(long i = 0; i < C; i++) tab = t; }
Vector() { for(long i = 0; i < C; i++) tab = 0; }
//template??? friend ostream & operator << (ostream &, Vector<T, C> &);
Vector<T, C> & operator = (Vector<T, C> &v) {
for(long i = 0; i < C; i++) tab = v;
return *this;
}
T operator [] (long i) { return tab; }
Vector(Vector<T, C> &v) { // copy constructor
for(long i = 0; i < C; i++) tab = v;
}
void i() { for(long i = 0; i < C; i++) if ( i == 0 ) tab = 1; else
tab = 0; }
void j() { for(long i = 0; i < C; i++) if ( i == 1 ) tab = 1; else
tab = 0; }
void k() { for(long i = 0; i < C; i++) if ( i == 2 ) tab = 1; else
tab = 0; }
Vector<T, C> operator * (T e);
};
template<class T, long C>
Vector<T, C> Vecteur<T, C>:
Vector<T, C> v(*this);
for(long i = 0; i < C; i++) v.tab = tab * e;
return v;
}
template <class T, long Ci, long Cj> // line, column
class Matrix { // tableau de vecteurs sur plusieurs lignes (ou en colonne
trans())
public:
//friend class Vector;
Vector<T, Cj> mat[Ci];
Matrix(Vecteur<T, Cj> tabv[]) {
for(long i = 0; i < Ci; i++) mat = tabv;
}
Matrix() {
for(long i = 0; i < Ci; i++) {
Vector<T, Cj> v;
mat = v;
}
}
Matrix(Vector<T, Cj> v) {
mat[0] = v;
for(long i = 1; i < Ci; i++) {
Vector<T, Cj> v;
mat = v;
}
}
Matrix<T, Ci, Cj> & operator = (Matrix<T, Ci, Cj> &m) {
for(long i = 0; i < Ci; i++) mat = m;
return *this;
}
Matrix<T, Ci, Cj> & operator = (Vector<T, Cj> &v) { mat[0] = v; return
*this; }
Vector<T, Cj> & operator [] (long i) { return mat; }
Matrix(Matrix<T, Ci, Cj> &m) { // copy constructor
for(long i = 0; i < Ci; i++) mat = m;
}
void transpose(); //modify for moment, not exactly what we want
void identity(); // modify
};
template <class T, long Ci, long Cj>
void Matrix<T, Ci, Cj>::transpose() {
for(long i = 0; i < Ci; i++)
for(long j = i+1; j < Cj; j++) {
T elt= mat[j];
//printf("i%d, j%d : elt %d [][]%d\n", i,j, elt, mat[j]);
mat[j].tab = mat[j];
mat.tab[j] = elt;
}
}
template <class T, long Ci, long Cj>
void Matrix<T, Ci, Cj>::identity() {
for(long i = 0; i < Ci; i++) {
for(long j = 0; j < Cj; j++)
if (i == j) mat.tab[j] = 1; else mat.tab[j] = 0;
}
}
template <class T, long C>
ostream & operator << (ostream &o, Vector<T, C> &v) {
o<<"[";
for(long i = 0; i < C; i++) {
o<<v.tab;
if (i+1 < C) o<<"\t";
}
o<<"]";
return o;
}
template <class T, long Ci, long Cj>
ostream & operator << (ostream &o, Matrix<T, Ci, Cj> &m) {
o<<"[";
for(long i = 0; i < Ci; i++) {
if (i == 0) ; else o<<" ";
o<<m;
if (i+1 < Ci) o<<"\n";
}
o<<"]";
return o;
}
template <class T, long C>
istream & operator >> (istream &is, Vector<T, C> &v) {
char c;
is>>c;
if (c != '[') {
cerr<<"Enter Vector between brackets '[' "<<v;
exit(1);
}
for(long i = 0; i < C; i++) {
is>>v.tab;
}
is>>c;
if (c != ']') {
cerr<<"Enter Vector between brackets ']' "<<v;
exit(1);
}
return is;
}
#endif
-------------------------
vec.h is the definition classes' file
v2.cpp the small test program.
Thank you a lot, helping me.
Gilles DRIDI
http://cdridi.club.fr