J'ai fait ma propre classe de cordes (c'est-à-dire pour les devoirs évidemment) et j'obtiens des erreurs de syntaxe bizarres sur deux de mes opérateurs. Mes opérateurs d'égalité et d'ajout affirment que j'ai trop de paramètres (c'est-à-dire dans mon fichier .h), mais prétendent que la méthode n'appartient même pas à la classe dans mon fichier .cpp!Opérateur (s) trop de paramètres pour cette fonction?
J'ai même fait de l'opérateur d'égalité un ami, mais l'intellisense me donne toujours les mêmes messages d'erreur.
Est-ce que quelqu'un sait ce que je fais mal ??
friend bool operator==(String const & left, String const & right);
string.h
bool operator==(String const & left, String const & right);
String const operator+(String const & lhs, String const & rhs);
string.cpp
bool String::operator==(String const & left, String const &right)
{
return !strcmp(left.mStr, right.mStr);
}
String const String::operator+(String const & lhs, String const & rhs)
{
//Find the length of the left and right hand sides of the add operator
int lengthLhs = strlen(lhs.mStr);
int lengthRhs = strlen(rhs.mStr);
//Allocate space for the left and right hand sides (i.e. plus the null)
char * buffer = new char[lhs.mStr + rhs.mStr + 1];
//Copy left hand side into buffer
strcpy(buffer, lhs.mStr);
//Concatenate right hand side into buffer
strcat(buffer, rhs.mStr);
//Create new string
String newString(buffer);
//Delete buffer
delete [] buffer;
return newString;
}
Même chose pour 'operator +', que ce soit 'ami' ou ennemi. – juanchopanza
@ NPE - Wow. Merci beaucoup!! – MrPickle5
EDIT: J'ai oublié de supprimer la déclaration de méthode INSIDE de ma classe. Tout est bien. Merci encore!! – MrPickle5