Problème d'opérateur de classe
Bonjour,
Ça fait un moment que je planche sur le problème, mais je n'arrive pas à m'en défaire. Avant toute remarque voici mon code source:
fichier de la classe:
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 53 54 55
| #include "Entier.h"
Entier::Entier(std::string a): m_nombre(a), std::string()
{
std::cout << a << std::endl;
}
Entier::Entier(): m_nombre(""), std::string()
{}
Entier& Entier::operator+=(const Entier& nombre2)
{
int i, retenue=0, tmp, taille, nb1, nb2;
for(i=0; i<(taille=std::max(nombre2.size(), m_nombre.size())); i++)
{
nb1=m_nombre[i]-'0';
std::cout << "nb1=" << m_nombre[i] << std::endl;
nb2=nombre2[i]-'0';
std::cout << "nb2=" << nombre2[i] << std::endl << "taille=" << nombre2.size() << std::endl;
if((tmp=nb2+nb1+retenue)>=10)
{
retenue=1;
tmp=tmp-10;
std::cout << "m_nombre[i]=" << m_nombre[i] << std::endl;
m_nombre[i]=((char)tmp+'0');
retenue=0;
std::cout << m_nombre[i] << std::endl;
}
else
{
retenue=0;
m_nombre[i]=((char)tmp+'0');
std::cout << m_nombre[i] << std::endl;
}
}
if(taille>m_nombre.size())
{
for(; i<taille; i++)
{
m_nombre.push_back(nombre2[i]);
nb1=m_nombre[i]-'0';
nb1++;
m_nombre[i]=((char)nb1+'0');
}
}
return *this;
}
Entier operator+(Entier const& a, Entier const& b)
{
Entier copie(a);
copie+=b;
return copie;
} |
fichier header
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <cstring>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
class Entier : public std::string
{
public:
Entier();
Entier(std::string);
Entier& operator+=(Entier const& nombre2);
private:
std::string m_nombre;
};
Entier operator+(Entier const&, Entier const&); |
Fichier main
Code:
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream>
#include "Entier.h"
using namespace std;
int main(int argc, const char *argv[])
{
Entier nb1("333");
Entier nb2("666");
nb1+nb2;
} |
Ainsi, dans la méthode operator+= lorsque j'affiche nombre2[i], il n'affiche rien du tout(juste un espace blanc) et lorsque je souhaite afficher la taille, il indique tout simplement 0.
Merci de toutes réponses