const et objet temporaire
Bonjour,
Voici le code sur lequel je travaille :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
template<typename T>
class vector_t
{
public:
virtual T const & operator[](size_t y) const = 0;
virtual T & operator[](size_t y) = 0;
virtual size_t size() const = 0;
};
template<typename T>
class Vector : public vector_t<T>
{
public:
Vector(size_t n) : n_(n), data_(n) { }
Vector(size_t n, T val) : n_(n), data_(n, val) { }
virtual T const & operator[](size_t y) const { return data_[y]; }
virtual T & operator[](size_t y) { return data_[y]; }
virtual size_t size() const { return data_.n_; }
vector<T> & data() { return data_; }
private:
size_t n_;
vector<T> data_;
};
template<typename T>
class Field
{
public:
class Field2 : public vector_t<T>
{
public:
Field2(Field & parent, size_t x) : parent_(parent), x_(x) { }
virtual T const & operator[](size_t y) const { return parent_.data_[x_ * parent_.n_ + y]; }
virtual T & operator[](size_t y) { return parent_.data_[x_ * parent_.n_ + y]; }
virtual size_t size() const { return parent_.n_; }
private:
Field & parent_;
size_t x_;
};
Field(size_t m, size_t n) : m_(m), n_(n), data_(m * n) { };
Field(size_t m, size_t n, T val) : m_(m), n_(n), data_(m * n, val) { };
Field2 const operator[](size_t x) const { return Field2(*this, x); };
Field2 operator[](size_t x) { return Field2(*this, x); };
size_t m() const { return m_; }
size_t n() const { return n_; }
vector<T> & data() { return data_; }
private:
friend class Field2;
size_t m_, n_;
vector<T> data_;
}; |
L'objectif de la classe Field est de permettre d'appliquer deux fois l'opérateur [] à un vecteur représentant une matrice.
Je souhaite exécuter la fonction :
Code:
1 2 3 4 5 6 7 8 9
|
void printField(const ScalarField & p)
{
for(size_t i = 0; i < p.m(); ++i) {
for(size_t j = 0; j < p.n() - 1; ++j)
cout << p[i][j] << '\t';
cout << p[i][p.n()-1];
if(i < p.m() - 1) cout << endl; }
} |
Dans le cas où il n'y a pas de const devant l'argument, la fonction fonctionne correctement. Lorsque le const est là, j'obtiens l'erreur suivante :
Citation:
math.hpp:63:66: error: no matching function for call to ‘Field<double>::Field2::Field2(const Field<double>&, size_t&)’
math.hpp:63:66: note: candidates are:
math.hpp:53:3: note: Field<T>::Field2::Field2(Field<T>&, size_t) [with T = double; Field<T> = Field<double>; size_t = long unsigned int]
math.hpp:53:3: note: no known conversion for argument 1 from ‘const Field<double>’ to ‘Field<double>&’
math.hpp:50:8: note: Field<double>::Field2::Field2(const Field<double>::Field2&)
math.hpp:50:8: note: candidate expects 1 argument, 2 provided
Je comprends que lorsqu'un objet est passé en const à une fonction, rien dans le corps de cette fonction ne doit altérer l'objet. Pour s'en assurer, toutes les fonctions appelées à partir de l'objet doivent être déclarées const.
Malheureusement, je ne vois pas comment régler ce problème... :?
En vous remerciant d'avance,
Nicolas.