bonjour

je fais un programme sous visual c++ 6.0 et j'obtiens des messages d'errors.
j'avais un problème d'opérateurs.

pourriez vous m'aider pour le programme suivant svp?

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
 
#include <string.h> // strcpy(); strlen();
#include <conio.h> // getch();
#include <iostream> // cout et cin
using namespace std;
 
class String
{
// private :
    int nbr;
    char *ptr;
public:
    String (char[]);
    String (char);
    ~String();
    String& operator=(const String &);
    String(const String &);
  /*  String plus (const String &); */
    String operator+(char);
};                                   
 
String String::operator+(char c)
{
    char*chaine=new char[nbr+1];
    for(int i=0;i<nbr-1;i++)
        chaine[i]=ptr[i];
    chaine[nbr-1]=c;
    chaine[nbr]='\0';
    String resultat(chaine);
    delete chaine;
    return(resultat);
}
 
void main ()
{
    String s1("azerty"),s2('q');
 
    String s3=s1+s2; // erreur ici
 
   /* s3=s1+'w'; cette ligne là fonctionne*/
    getch(); 
}
l'erreur du compilateur est la suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'class String' (or there is no acceptable conversion)
 
error C2512: 'String' : no appropriate default constructor available
ce sont les 2 erreurs et normalement on dit bien s1+s2 qui vont donner un nouveau String s3.

Si j'essaye avec devc++, il me dit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 no match for 'operator+' in 's1 + s2' 
 candidates are: String String::operator+(char)
Comme ça marche avec s1+'w' donc l'opérateur "+" fonctionne, de là s1+s2 devrait marcher
ptet que ça ne marche pas parce que s2 est un objet ayant 'w' et non un caractère mais alors comment modifier la fonction operator+ pour que ça marche aussi avec +s2 et aussi avec +'w' ?

Merci