|
|
|
|
Bonjour,
Je me suis bloqué sur cette methode:
Chaine Chaine::operator+(const Chaine &c){
Chaine n;
n.longueur=longueur+c.longueur;
n.ch=new char[n.longueur];
for(int i=0;i<n.longueur;i++)
n.ch=ch[i]+c.ch[i];
return n;
}
int main(int argc, char *argv[])
{
int egalite;
Chaine c("bla"),r("blo");
Chaine t;
t=c+r;
t.affichage();
system("PAUSE");
return EXIT_SUCCESS;
}
Configuration: Windows XP Firefox 3.0.5
Répondre à fiddy
|
J'ai essayé la fonction strcat ça marchait, mais avec un cout dans la methode operator+, le probleme maintenant dans le main:
int main(int argc, char *argv[])
{
int egalite;
Chaine c("bla"),r(" blo");
Chaine t;
t=c+r; //
system("PAUSE");
return EXIT_SUCCESS;
}
sachant que j'ai definit un constructeur par defaut (sans arguments) et la nouvelle mathode operator+: Chaine Chaine::operator+(const Chaine &c){
Chaine n;
n.longueur=longueur+c.longueur;
n.ch=new char[n.longueur];
strcpy(n.ch,strcat(ch,c.ch));
return n;
}
erreur: In function `int main(int, char**)': no matching function for call to `Chaine::Chaine(Chaine)' candidates are: Chaine::Chaine(Chaine&) Chaine::Chaine(char*) |
Déjà tu pourrais coder le constructeur par recopie, ça t'éviterait l'utilisation de strncpy.
Chaine Chaine::operator+(const Chaine &a){
Chaine r;
r.longueur=a.longueur+Chaine::longueur;
r.ch=new char[r.longueur+1];
strncpy(r.ch,Chaine::ch,r.longueur);
strncat(r.ch,a.ch,r.longueur);
return r;
}
J'ai mis r.n+1 en supposant que tu n'as pas compté le '\0' dans longueur. Google is your friend |
Toujours cette erreur:
In function `int main(int, char**)': no matching function for call to `Chaine::Chaine(Chaine)' candidates are: Chaine::Chaine(Chaine&) Chaine::Chaine(char*) dans le main: int main(int argc, char *argv[])
{
int egalite;
Chaine c("bla"),r(" blo");
Chaine t;
t=c+r;
system("PAUSE");
return EXIT_SUCCESS;
} |
Ok, donc je pense que le problème vient du fait que tu n'as définit : Chaine(char*).
|
Problème de constructeur de recopie, si je l'élimine tout marche bien, si je le garde --->les erreurs
class Chaine{
private:
int longueur;
char *ch;
public:
Chaine();
Chaine(char*);
//Chaine(Chaine&);
~Chaine();
void affichage();
Chaine& operator=(const Chaine&);
Chaine operator+(const Chaine&);
};
Classe Chaine.cpp: Chaine::Chaine(){
longueur=0;
ch=NULL;
}
Chaine::Chaine(char *c){
longueur=strlen(c)+1;
ch=new char[longueur];
strcpy(ch,c);
}
/*Chaine::Chaine(Chaine &c){
longueur=c.longueur;
ch=new char[longueur+1];
strcpy(ch,c.ch);
}*/
Chaine::~Chaine(){
if(ch)
delete[] ch;
}
void Chaine::affichage(){
cout<<"Votre chaine est :"<<ch<<endl;
}
Chaine& Chaine::operator=(const Chaine &c){
if(ch)
delete[] ch;
ch=new char[longueur+1];
strcpy(ch,c.ch);
return *this;
}
Chaine Chaine::operator+(const Chaine &c){
Chaine n;
n.longueur=longueur+c.longueur;
n.ch=new char[n.longueur+1]; //ou +2 puisque j'ai 2 chaines "ché pa"
strcpy(n.ch,strcat(ch,c.ch));
return n;
}
L'inconvénient de strcat c'est qu'il modifie la chaine initiale(ch) on ajoutant le c.ch Par exemple si j'ai 2 chaines a("hello"); et b(" world"); donc aprés le a+b; automatiquement a devient: a("hello world"). Dans ce cas si j'ai crée une autre instance Chaine c; et je fais c=a+b; le resultat est toujours "hello world", ce qui explique b n'est pas lu ==>operator+ est desactivé juste l'affectation qui marche là. Bon voila mon main: int main(int argc, char *argv[])
{
Chaine c("bla"),r(" blo"),k;
k=c+r;
c.affichage();
system("PAUSE");
return EXIT_SUCCESS;
}
p.s. j'ai pas ecris les includes et les namespace. |