Moderate (beginner) elaborate template (imbricated Vector & Matrix) with VS2003 & G++ 3.4.4

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>::eek:perator = (Vecteur<T, C> &) give
"no match for 'operator =' in 'v = Vecteur<T, C>::eek: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>::eek: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
 
P

Paavo Helde

Vecteur<int, DIM> v1, v; [...]
/* == error here === */
v= (v1*3); [...]

template <class T, long C>
class Vecteur {
public: [...]
Vecteur<T, C> & operator = (Vecteur<T, C> &v) {

This should be in FAQ, but I could not find it in 3 minutes. You are
missing const correctness. The expression (v1*3) yields a temporary which
cannot bind to the non-const reference in your assignment operator. The
signature of the assignment operator should be
Vecteur<T, C> & operator = (const Vecteur<T, C> &v);


There are several problems in your code:

* const-s are missing all over the place
* class data is not in the private section
* the class names Vector and Vecteur are indistinct

hth
Paavo
 
G

Gilles DRIDI

Thank you a lot Paolo.

The compiler don't say clearly it is a const suject ..

All you want to know is that a temporary object is const.
Thus, the temporary object from the + operator must be manipulated by const
parameter (first paramenter) for
1. the copy constructor : X(const X &)
2. the = operator
Moreover, the access operator [] must be declared as a const function (I do
not know how to declare such a function)
operator [] (long i) const { return tab; }

This is for your guidance for programmer beginner with template.
Gilles DRIDI
http://cdridi.club.fr

Paavo Helde said:
Vecteur<int, DIM> v1, v; [...]
/* == error here === */
v= (v1*3); [...]

template <class T, long C>
class Vecteur {
public: [...]
Vecteur<T, C> & operator = (Vecteur<T, C> &v) {

This should be in FAQ, but I could not find it in 3 minutes. You are
missing const correctness. The expression (v1*3) yields a temporary which
cannot bind to the non-const reference in your assignment operator. The
signature of the assignment operator should be
Vecteur<T, C> & operator = (const Vecteur<T, C> &v);


There are several problems in your code:

* const-s are missing all over the place
* class data is not in the private section
* the class names Vector and Vecteur are indistinct

hth
Paavo
 
J

James Kanze

Thank you a lot Paolo.
The compiler don't say clearly it is a const suject ..
All you want to know is that a temporary object is const.

No it's not. As Paavo said, the language doesn't permit the
initialisation of a non-const reference with a temporary.
Temporaries themselves are not const, unless they've been
explicitly declared to be const.
Thus, the temporary object from the + operator must be
manipulated by const parameter (first paramenter) for
1. the copy constructor : X(const X &)
2. the = operator

The basic rule is to conform to what the compiler does by
default. That means const references as parameters for both the
copy constructor and the copy assignment operator. Without
that, you cannot copy temporaries or const variables.

This is, of course, a standard idiom, which should be explained
in any introductory book to C++. (The exact rules concerning
how overload resolution, temporaries and const interact are
relatively complicated. Just use const anytime you don't modify
the object, however, and the results will work in a fairly
simple and intuitive manner, and you can more or less ignore the
exact rules.)
Moreover, the access operator [] must be declared as a const
function (I do not know how to declare such a function)
operator [] (long i) const { return tab; }


Usually, there are two versions of the operator[], one const,
and the other not.
This is for your guidance for programmer beginner with template.
Gilles DRIDIhttp://cdridi.club.fr

I didn't find any explinations of templates there. (You might
also warn that it is in French---posters here aren't necessarily
fluent in the language. Also, I think you'll find that most
professional programmers disagree with your "adage" at the end.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top