Bonjour !
Je rencontre un problème sur un code particulier, et le code minimal n'est pas plus aidant.
Il s'agit d'une classe (ici nommée objet) ayant pour seul attribut une std::string.
L'objectif étant de pouvoir avoir accès à un caractère de cette string via l'objet et son opérateur [] : objet.string[i] <=> objet[i]
Seulement, l'utilisation de l'opérateur [] pose problème dans le cadre des fonctions externes à l'objet.
En effet, si monObjet[i] marche très bien dans le main, cela n'est pas valable pour la fonction objet2str, qui tente d'employer l'opérateur [] et qui n'y arrive pas.
compilation :
La totalité des fichiers minimaux sont ici :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 g++ -Wall -Werror -c *.cpp objet.cpp: In function std::string objet2str(const objet*): objet.cpp:41:33: error: no match for operator+ in *(((const objet*)obj) + ((sizetype)(((long unsigned int)i) * 8ul))) + '0'
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 #include <iostream> #include <cstdlib> #include <fstream> #include <sstream> #include <string> // LOCAL MODULES #include "objet.h" int main(int argc, char* argv[]) { objet obj("101010"); std::cout << obj[0] << std::endl; return EXIT_SUCCESS; }
objet.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 #include "objet.h" /*************************************************** * CONSTRUCTOR ***************************************************/ objet::objet(const std::string sequence) { value = sequence; } /*************************************************** * OPERATORS ***************************************************/ char objet::operator[](const unsigned int id) const { return value[id]; } /*************************************************** * OTHER FUNCTIONS ***************************************************/ /* * CONVERSIONS */ std::string objet2str(const objet* const obj) { std::string str = ""; for(unsigned int i = 0; i < obj->len(); i++) { str += (obj[i])+'0'; // ERREUR // Il semblerait que obj[i] renvois un objet, non un char ! } return str; }
objet.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 #ifndef objet_H_INCLUDED #define objet_H_INCLUDED /* * LIBRARIES */ // STD LIB #include <iostream> #include <cstdlib> #include <string> /* * objet CLASS */ class objet { public: // CONSTRUCTOR objet(const std::string); // ACCESSORS unsigned int len() const { return value.size(); } // OPERATORS char operator[](const unsigned int) const; private: // ATTRIBUTES std::string value; }; /* * OTHER FUNCTIONS */ std::string objet2str(const objet*const); #endif
Lorsque la ligne problématique est retirée, le main fonctionne pour ainsi dire parfaitement. (obj[0] renvois bien 1)
En vous remerciant de votre aide.
PS: ce code est un code minimal. Les algorithmes mis en oeuvre sont donc souvent illogiques tels quels.
Partager