IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

cannot access private member declared in class 'Matrice'


Sujet :

C++

  1. #1
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access private member declared in class 'Matrice'
    bonjour, voila qui peut m'aider !

    marice.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <iostream>
     
    using namespace std;
     
     
    class Matrice
    {
       private:
          typedef double *ligne;
          ligne *lignes;
          unsigned int n; // Nombre de lignes (1er paramètre)
          unsigned int m; // Nombre de colonnes (2ème paramètre)
          void echange(double &a, double &b) const;
          Matrice supligne(const Matrice &mat, unsigned int lig, unsigned int col) const;
          double det(const Matrice &mat) const;
          float expo(unsigned int n) const;
     
       public:
          //les constructeurs/destructeur
          Matrice();
          Matrice(unsigned int nl, unsigned int nc);
          Matrice(unsigned int nl, unsigned int nc, double valeur);
          Matrice(const Matrice &source);
          ~Matrice(void);
          //les operateurs
          Matrice &operator=(const Matrice &mat);
          double &operator()(unsigned int i, unsigned int j);
          Matrice operator+(const Matrice &mat) const;
          Matrice operator-(const Matrice &mat) const;
          Matrice operator*(const Matrice &mat) const;
          Matrice operator*(const double nb) const;
          friend Matrice operator*(const double nb,const Matrice &mat);
          Matrice & operator*=(const Matrice &mat);
          Matrice & operator*=(const double nb);
          Matrice & operator+=(const Matrice &mat);
          Matrice & operator-=(const Matrice &mat);
          Matrice & operator/=(const double);
          Matrice  operator/(const double) const;
          //accesseur et manipulations
          Matrice t();
          double trace() const;
          double det() const;
          Matrice co() const;
          Matrice inverse() const;
          int dim() const;
          unsigned int nb_colones() const;
          unsigned int nb_lignes() const;
          friend ostream & operator<<(ostream &os,const Matrice &mat);
     
    };
    main.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    #include "matrice.h"
     
    void test_constructeurs()
    {
       Matrice m1(3,2,4);
       Matrice m2(3,8,3);
       Matrice m3(2,2,9);
       Matrice m4(m1);
       Matrice m5;
       Matrice m6(2,2);
     
       cout << "Test des constructeurs :" << endl;
       cout << "========================" << endl << endl;
     
       cout << "Matrice m1(3,2,4)" << endl;
       cout << m1 << endl;
       cout << "Matrice m2(3,8,3)" << endl;
       cout << m2 << endl;
       cout << "Matrice m3(2,2,9)" << endl;
       cout << m3<< endl;
       cout << "Matrice m4(m1)" << endl;
       cout << m4 << endl;
       cout << "Matrice m5" << endl;
       cout << m5 << endl;
       cout << "Matrice m6(2,2)" << endl;
       cout << m6 << endl;
    }
     
    void test_operateurs()
    {
       Matrice m1(3,2,4);
       Matrice m2(3,8,3);
       Matrice m3(2,2,9);
       Matrice m4;
     
       cout << "Test des operateurs :" << endl;
       cout << "=====================" << endl << endl;
     
       cout << "m1+m2" << endl << (m1+m2) << endl;
       cout << "m1-m2" << endl << (m1-m2) << endl;
     
       {
             cout << "m1*m3" << endl << (m1*m3) << endl;
       }
     
       cout << "m1*2" << endl << (m1*2) << endl;
       cout << "2*m1" << endl << (2*m1) << endl;
       cout << "m4=m3" << endl << (m1=m3) << endl;  
     
       {
             cout << "m1*=m3" << endl << (m1*=m3) << endl;
       }
     
       cout << "m1*=2" << endl << (m1*=2) << endl;
       cout << "m1/=2" << endl << (m1/=2) << endl;
       cout << "m1+=m2" << endl << (m1+=m2) << endl;
       cout << "m1-=m2" << endl << (m1-=m2) << endl;      
    }
     
    void test_manip()
    {
       Matrice m1(3,2,4);
       Matrice m2(3,8,3);
       Matrice m3(2,2,9);
     
       cout << "Test des Manipulations :" << endl;
       cout << "========================" << endl << endl;
     
       cout << "m1 : " << endl << m1 << endl << "transposee de m1" << endl << m1.t() << endl;
     
       {
             cout << "trace de m3 : " << endl << m3.trace() << endl;
       }
     
       {
             cout << "det(m3) : " << endl << m3.det() << endl;
       }
     
       m3(0,1)=2;
       m3(1,1)=-3;
     
       {
             cout << "co(m3) : " << endl << m3.co() << endl;
       }
     
       {
             cout << "inverse(m3) : " << endl << m3.inverse() << endl;
       }
     
       cout << "m3 possede " << m3.nb_lignes() << " lignes et " << m3.nb_colones() << " colones" << endl;
     
    }
     
    int main()
    {
      test_constructeurs();
      test_operateurs();
      test_manip();
     
       Matrice m4(4,4,0);
       m4(0,0)=0;m4(0,1)=1;m4(0,2)=2;m4(0,3)=3;
       m4(1,0)=4;m4(1,1)=5;m4(1,2)=5;m4(1,3)=6;
       m4(2,0)=7;m4(2,1)=8;m4(2,2)=9;m4(2,3)=0;
       m4(3,0)=4;m4(3,1)=3;m4(3,2)=2;m4(3,3)=1;
     
       //on peut combiner les opérateurs  : 
     
       cout << endl <<  (((m4=(m4*=m4))*m4)/=2)-m4 << endl;
       cout << ((((m4=(m4*=m4))*m4)/=2)-m4).inverse() << endl;
     
       cout << "det(t(A)) == det(A)" << endl;
       cout << endl << m4.t().det() << endl;
       cout << m4.det() << endl;
     
     
      return 0;
    }
    les erreurs !
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    matrice.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(320) : error C2248: 'n' : cannot access private member declared in class 'Matrice'
            c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(11) : see declaration of 'n'
    C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(322) : error C2248: 'm' : cannot access private member declared in class 'Matrice'
            c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(12) : see declaration of 'm'
    C:\Program Files\Microsoft Visual Studio\MyProjects\projet2c\matrice.cpp(323) : error C2248: 'lignes' : cannot access private member declared in class 'Matrice'
            c:\program files\microsoft visual studio\myprojects\projet2c\matrice.h(10) : see declaration of 'lignes'
    Error executing cl.exe.
    matrice.cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    // +--------------------------------------------------------------------------+
    // | Fichier   : matrix.cpp                                                   |
    // | Utilite   : définition de la Matrice.                                    |
    // | Auteurs   : Nicolas AUNAI                                                |
    // | Creation  : 16.01.2003                                                   |
    // | Remarques : aucune.                                                      |
    // +--------------------------------------------------------------------------+
     
    #include "matrice.h"
     
    void 
    Matrice::echange(double &a,double &b) const
    {
       a+=b;
       b=a-b;
       a-=b;
    }
     
    Matrice::Matrice()
    {
       n = 0;
       m = 0;
       lignes = NULL;
    }
     
    Matrice::Matrice(unsigned int nl, unsigned int nc)
    {
       n = nl;
       m = nc;
       lignes = new ligne[n];  // allocations
       for (unsigned int i=0; i<n; i++)
       {   
          lignes[i] = new double[m];
          for(unsigned int j=0; j<m; j ++)
             lignes[i][j] = 0;  //initialisation à '0'
       }
    }
     
    Matrice::Matrice(const Matrice &source)
    {
       n = source.n;
       m = source.m;
     
       lignes = new ligne[n];
       for(unsigned int i=0; i<n; i ++)
       {   
          lignes[i] = new double[m];
          for(unsigned int j=0; j<m; j ++)
             lignes[i][j] = source.lignes[i][j]; //recopie
       }
     
    }
     
    Matrice::Matrice(unsigned int nl, unsigned int nc, double valeur)
    {
       n = nl;
       m = nc;
     
       lignes = new ligne[n];
       for(unsigned int i=0; i<n; i ++)
       {   
          lignes[i] = new double[m];
          for(unsigned int j=0; j<m; j ++)
             lignes[i][j] = valeur; //initialisation à 'valeur'
       }
    }
     
    Matrice::~Matrice()
    {
       for(unsigned int i=0; i<n; i ++) //destruction
          delete[] lignes[i];
     
       delete[] lignes;
    }
     
    Matrice &
    Matrice::operator=(const Matrice &mat)
    {
       if(this != &mat) //assignation M = M impossible
       {
          if(mat.n != n || mat.m != m) // si tailles pas compatibles
          {
             for(unsigned int i=0; i<n; i++) // on détruit...
                delete[] lignes[i];
             delete[] lignes;
     
             n = mat.n;
             m = mat.m,
     
             lignes = new ligne[n];   // on réalloue
             for(i=0; i<n; i++)
                lignes[i] = new double[m];
          }
     
          for(unsigned int i=0; i<n; i ++)
             for(unsigned int j=0; j<m; j ++)
                lignes[i][j] = mat.lignes[i][j]; // et on copie...
       }
       return *this;
    }
     
    double &
    Matrice::operator()(unsigned int i, unsigned int j)
    {
       return lignes[i][j];    
    }
     
    Matrice
    Matrice::operator+(const Matrice &mat) const
    {
      Matrice tmp(n,m);
       for (unsigned int i=0; i<n; i++) // Double boucle.
          for (unsigned int j=0; j<m; j++)
             tmp.lignes[i][j] = lignes[i][j] + mat.lignes[i][j]; //addition
     
       return tmp; 
    }
     
    Matrice 
    Matrice::operator-(const Matrice &mat) const
    {
       Matrice tmp(n,m);
       for (unsigned int i=0; i<n; i++) // Double boucle.
          for (unsigned int j=0; j<m; j++)
             tmp.lignes[i][j] = lignes[i][j] - mat.lignes[i][j]; //soustraction
     
       return tmp;
    }
     
    Matrice 
    Matrice::operator*(const double nb) const
    {
       Matrice tmp(n,m);
       for (unsigned int i=0; i<n; i++) // Double boucle.
          for (unsigned int j=0; j<m; j++)
             tmp.lignes[i][j] = lignes[i][j] * nb; // produit
     
       return tmp;
    }
     
    Matrice 
    Matrice::operator*(const Matrice &mat) const
    {
       Matrice tmp(n,mat.m,0);
       if(m == mat.n) // si nb_colone == nb_ligne de l'autre matrice...
       {
          for(unsigned int i=0; i<n; i++) //pour chq ligne...
             for(unsigned int j=0; j<mat.m; j++) // pour chaque colone
             {
                for(unsigned int k=0; k<m; k++) // produit scalaire
                   tmp.lignes[i][j] += lignes[i][k]*mat.lignes[k][j];
             }
       }
     
       return tmp;
    }
     
    Matrice 
    operator*(const double nb,const Matrice &mat)
    {
       return mat*nb;
    }
     
    Matrice & 
    Matrice::operator*=(const double nb)
    {
       Matrice tmp(n,m,0);
       for(unsigned int i=0; i<n; i++) // Double boucle.
             for(unsigned int j=0; j<m; j++)
                tmp.lignes[i][j] = lignes[i][j]*nb; //produit
     
          *this = tmp;
     
       return *this;
    }
     
    Matrice &
    Matrice::operator*=(const Matrice &mat)
    {
       Matrice tmp(n,mat.m,0);
       if(m == mat.n) //si multiplication possible....
       {   
          for(unsigned int i=0; i<n; i++)
             for(unsigned int j=0; j<mat.m; j++)
             {
                for(unsigned int k=0; k<m; k++)
                   tmp.lignes[i][j] += lignes[i][k]*mat.lignes[k][j];
             }
          *this = tmp;
       }
     
       return *this;
    }
     
    Matrice &
    Matrice::operator+=(const Matrice &mat)
    {
       for (unsigned int i=0; i<n; i++) // Double boucle.
          for (unsigned int j=0; j<m; j++)
             lignes[i][j] += mat.lignes[i][j]; // +=
     
       return *this; 
    }
     
    Matrice &
    Matrice::operator-=(const Matrice &mat)
    {
       for (unsigned int i=0; i<n; i++) // Double boucle.
          for (unsigned int j=0; j<m; j++)
             lignes[i][j] -= mat.lignes[i][j]; //-=
     
       return *this; 
    }
     
    Matrice 
    Matrice::operator/(const double nb) const
    {
       Matrice tmp = *this;
       if(nb) 
       {
          for(unsigned int i=0; i<n; i++)
             for(unsigned int j=0; j<n; j++)
                tmp.lignes[i][j]/=nb;
       }
     
       return tmp;
    }
     
    Matrice &
    Matrice::operator/=(const double nb)
    {
       if(nb)
       {
          for(unsigned int i=0; i<n; i++)
             for(unsigned int j=0; j<n; j++)
                lignes[i][j]/=nb;
       }
     
       return *this;
    }
     
    Matrice
    Matrice::t()
    {
       Matrice tmp(m,n);
       for(unsigned i=0; i<n; i++)
          for(unsigned j=0; j<m; j++)
             tmp.lignes[j][i]=lignes[i][j];
       return tmp;
    }
     
    double
    Matrice::trace() const
    {
       double sum=0;
       if(n == m) // trace => matrice carrée
       {
          for(unsigned int i=0; i<n; i++)
                for(unsigned int j=0; j<m; j++)
                   if(i == j)
                      sum+= lignes[i][j];
       }
     
       return sum;
    }
     
    Matrice 
    Matrice::supligne(const Matrice &mat, unsigned int lig, unsigned int col) const
    {
       unsigned int dim = mat.n;
       unsigned int ld = 0,cd=0;
       Matrice dest(mat.n-1,mat.n-1,0); // -1 ligne et -1 colone
       for (unsigned int L=0; L<dim; L++)
       {
          cd = 0; // revient a colone 0
          if(L != lig) // sauf ligne a virer
          {
             for(unsigned int c=0; c<dim; c++)
                if(c != col) // sauf colone a virer
                   dest.lignes[ld][cd++]=mat.lignes[L][c]; // recopie
             ld++;
          }
       }
       return dest;
    }
     
    float
    Matrice::expo(unsigned int n) const
    {
    if(!(n%2)){return (1);}
    return (-1);
    }
     
    double
    Matrice::det(const Matrice &mat) const
    {
       Matrice m2(mat.n,mat.n,0);
       double x=0;
     
       if(mat.n==1) //final
          return mat.lignes[0][0];
     
       for(unsigned int i=0; i<mat.n; i++)
       {
          m2 = supligne(mat,i,0); //extrait la sous matrice
          x=x+(expo(i)*mat.lignes[i][0]*det(m2)); 
       }
       return x;
    }
     
    double
    Matrice::det()const
    {
     
       return det(*this);
    }
     
    ostream & operator<<(ostream &os,const Matrice &mat)
    {
       for(unsigned int i=0; i<mat.n; i++)
       {
          for(unsigned int j=0; j<mat.m; j++)
             os <<mat.lignes[i][j] << " ";
     
          os << endl;
       }
     
       return os;
    }
     
    Matrice
    Matrice::co() const
    {
       Matrice m2(n,m,0);
       Matrice ret(n,m,0);
     
       if (n==1)
       {
          ret.lignes[0][0]=1;
       }
       else
       {
          for (unsigned int i=0;i<n;i++)
             for (unsigned int j=0;j<n;j++)
             {
                m2 = supligne(*this,i,j);// sous_matrice
                ret.lignes[i][j]=expo(i+j)*m2.det();
             }
       }
       return ret;
    }
     
     
     
    Matrice 
    Matrice::inverse() const
    {
       Matrice inv(n,m,0);
     
       inv = (this->co().t())/(this->det());
     
       return inv;
    }
     
    int
    Matrice::dim() const
    {
       return (n == m)?static_cast<int>(n):-1;
    }
     
    unsigned int
    Matrice::nb_colones() const
    {
       return m;
    }
     
    unsigned int
    Matrice::nb_lignes() const
    {
       return n;
    }

  2. #2
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Ca ressemble à un vieux bug de Visual C++ 6.
    Mais bon, tu n'as pas besoin de déclarer ton opérateur << ami de la classe, fais le plutôt utiliser les accesseurs qui vont bien, puisqu'ils sont définis.

  3. #3
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access private member declared in cl
    Merci, c'est quoi qui ressemble à un vieux C++ ? de plus vous m'avez dir d'enlever l'opérateur << mais lorsque je l'enleve tout les cout<<.....ne les reconnait pas, alors vous m'avez dit d'utiliser les acceseurs c'est quoi les accesseurs ?

  4. #4
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    c'est quoi qui ressemble à un vieux C++ ?
    J'ai pas dit "un vieux C++", mais "un vieux bug de Visual C++ 6". Je me souviens que dans certaines conditions, un opérateur << déclaré ami n'arrive tout de même pas à accéder aux données privées de la classe.

    de plus vous m'avez dir d'enlever l'opérateur << mais lorsque je l'enleve tout les cout<<.....ne les reconnait pas, alors vous m'avez dit d'utiliser les acceseurs c'est quoi les accesseurs ?
    J'ai pas dit d'enlever l'opérateur <<. J'ai juste dit que pour accéder aux élements de la matrice, tu peux utiliser les fonctions membres publiques qui existent déjà :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    double &operator()(unsigned int i, unsigned int j);
    unsigned int nb_colones() const;
    unsigned int nb_lignes() const;

  5. #5
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access private member declared in cl
    SVP Est ce que vous l'avez essayer avec ce que vous avez dit et ca marche !
    donc j vais pas declaré l'operateur << comme etant une fonction amie c'est ca, si j'i bien compris ou non ?

  6. #6
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    En enlevant la déclaration d'amitié entre la classe Matrice et l'opérateur <<, et en écrivant celui-ci comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ostream & operator<<(ostream &os,const Matrice &mat)
    {
       for(unsigned int i=0; i<mat.nb_lignes(); i++)
       {
          for(unsigned int j=0; j<mat.nb_colones(); j++)
             os <<mat(i, j) << " ";
     
          os << endl;
       }
     
       return os;
    }
    Et en ajoutant cette version de l'opérateur () (pour l'accès en lecture sur les objets constants) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    double operator()(unsigned int i, unsigned int j) const;
    ... Tu ne devrais plus avoir de problème.

    PS : si tu développes encore sous Visual C++ 6, pense sérieusement à changer de compilo. Il y en a plein de très bons, récents et gratuits.

    PS2 : colonne ça prend deux "n"

  7. #7
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access a private number
    Merci, pour la réponse mais j'ai changer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    friend ostream & operator<<(ostream &os,const Matrice &mat);
    par:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ostream & operator<<(ostream &os,const Matrice &mat);
    et par :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ostream & operator<<(ostream &os,const Matrice &mat)
    {
       for(unsigned int i=0; i<mat.nb_lignes(); i++)
       {
          for(unsigned int j=0; j<mat.nb_colones(); j++)
             os <<mat(i, j) << " ";
     
          os << endl;
       }
     
       return os;
    }
    et j'ai ajouter dans classe matrice.h:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    double operator()(unsigned int i, unsigned int j) const;
    mais par quoi je vais définir cet opérateur dans matrice.cpp.

    Mais malgré les corrections voilà les erreurs que j'ai il ne reconnait pas les << :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Compiling...
    main.cpp
    c:\program files\microsoft visual studio\myprojects\projet4\matrice.h(50) : error C2804: binary 'operator <<' has too many parameters
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(16) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(18) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(20) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(22) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(24) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(26) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(39) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(40) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(43) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(46) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(47) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(48) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(51) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(54) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(55) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(56) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(57) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(69) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(83) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(87) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(108) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    c:\program files\microsoft visual studio\myprojects\projet4\main.cpp(109) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrice' (or there is no acceptable conversion)
    Error executing cl.exe.
     
    main.obj - 23 error(s), 0 warning(s)
    je serais reconnaissante si vous me répondiez !

  8. #8
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut erreur de cmpliation
    merci !

  9. #9
    Rédacteur

    Avatar de millie
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    7 015
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 7 015
    Points : 9 818
    Points
    9 818
    Par défaut
    Citation Envoyé par Maria1505

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    double operator()(unsigned int i, unsigned int j) const;
    mais par quoi je vais définir cet opérateur dans matrice.cpp.
    Par le même code qu'il y avait dans l'opérateur : double& operator()...;
    Je ne répondrai à aucune question technique en privé

  10. #10
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Et si tu définis un opérateur << non ami dans Matrice, il le prendra pour une fonction membre... Il faut sortir la déclaration de la classe.

  11. #11
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access a private number
    Désolée laurent j'ai pas bien compris ce que vous avez dis, comment puis je définir l'opérateur << ?

  12. #12
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    ("déclarer", pas "définir")

    Comme n'importe quelle fonction non-membre, càd à l'extérieur de la classe.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    class Matrice
    {
        // ...
    };
     
    ostream& operator << (ostream& os, const Matrice& mat);

  13. #13
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access a private number
    Merci, cela a amélioré mon code, mais j"en ai encore des erreurs de linkage qui sont :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    matrice.obj : error LNK2001: unresolved external symbol "public: double __thiscall Matrice::operator()(unsigned int,unsigned int)const " (??RMatrice@@QBENII@Z)
    Debug/projet4.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    je crois que le probleme vient de l'operateur que vous m'avez dit d'ajouter


  14. #14
    Rédacteur

    Avatar de millie
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    7 015
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 7 015
    Points : 9 818
    Points
    9 818
    Par défaut
    Tu as dû oublier de définir l'opérateur (pas de problème lors de la compilation car il y a déclaration, mais problème lors de l'édition des liens).
    Je ne répondrai à aucune question technique en privé

  15. #15
    Membre régulier
    Inscrit en
    Novembre 2006
    Messages
    304
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations forums :
    Inscription : Novembre 2006
    Messages : 304
    Points : 77
    Points
    77
    Par défaut cannot access a private number
    merci beaucoup cela a résolu mes problemes, Chapeau Millie et Mille pour Laurent.Maria

  16. #16
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Pense à cliquer sur

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [javac] "cannot access" sur une classe chargée
    Par if_zen dans le forum Build
    Réponses: 1
    Dernier message: 05/10/2012, 23h13
  2. Réponses: 7
    Dernier message: 05/07/2010, 17h58
  3. Réponses: 7
    Dernier message: 16/06/2006, 11h09
  4. Declaré en classe de base, instancié en sous classe
    Par jobigoud dans le forum Langage
    Réponses: 3
    Dernier message: 14/11/2005, 10h55
  5. message : cannot add new member
    Par meli0207 dans le forum MFC
    Réponses: 3
    Dernier message: 06/06/2005, 14h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo