Convertir un entier en string

Résolu/Fermé
adeline11 Messages postés 54 Date d'inscription dimanche 28 septembre 2008 Statut Membre Dernière intervention 18 février 2010 - 6 févr. 2009 à 19:06
adeline11 Messages postés 54 Date d'inscription dimanche 28 septembre 2008 Statut Membre Dernière intervention 18 février 2010 - 24 févr. 2009 à 16:51
Bonjour,
ma question peut paraitre simpliste mais je souhaiterai convertir un entier en string (en C++), voila mon code:

string
Rationnel::toString()
{
string rationnel(my_num,"/",my_deno;
return ratio;
}

celà ne marche pas (ce qui n'est pas suprenant), voilà le message d'erreur:
"
Rationnel.cc: In member function «std::string Rationnel::toString()»:
Rationnel.cc:124: erreur: invalid conversion from «int» to «const char*»
Rationnel.cc:124: erreur: initializing argument 1 of «std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]»
Rationnel.cc:124: erreur: invalid conversion from «const char*» to «unsigned int»
Rationnel.cc:124: erreur: initializing argument 2 of «std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&, typename _Alloc::rebind<_CharT>::other::size_type, typename _Alloc::rebind<_CharT>::other::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]»
make: *** [Rationnel.o] Erreur 1
"
Merci d'avance pour votre aide ;p

4 réponses

mohamed-hedi Messages postés 10 Date d'inscription lundi 21 juillet 2008 Statut Membre Dernière intervention 19 février 2009 1
6 févr. 2009 à 19:11
c koi le code de ta fonction rationnel()
ca peu aider :)
0
adeline11 Messages postés 54 Date d'inscription dimanche 28 septembre 2008 Statut Membre Dernière intervention 18 février 2010 34
6 févr. 2009 à 19:14
Rationnel n'est pas une fonction c'est une classe:

class Rationnel {
private :
int my_num ;
int my_deno ;

public :
Rationnel();
Rationnel(int a, int b);
~Rationnel();

void affiche() const;
void setNum(int n);
void setDeno(int n);
void inverse();
bool operator==(const Rationnel & n);
void soustraction(const Rationnel & autre, Rationnel & difference);
void addition(const Rationnel & autre, Rationnel & somme);
void multiplication(const Rationnel & autre, Rationnel & produit);
void division(const Rationnel & autre, Rationnel & quotient);
void reduit();
string toString();

};

La fonction qui pose problème est la fonction toString, j'ai mis son code dans mon premier post
0
Nep_51 Messages postés 49 Date d'inscription jeudi 5 février 2009 Statut Membre Dernière intervention 7 avril 2009 9
6 févr. 2009 à 19:45
Bonjour,

Va faire un tour sur ce site tu trouvera peut-être ton bonheur:
https://cpp.developpez.com/faq/cpp/?page=Les-chaines-de-caracteres-std-string#STRINGS_numtostr


#include <sstream>

int main()
{    
    // créer un flux de sortie
    std::ostringstream oss;
    // écrire un nombre dans le flux
    oss << 10;
    // récupérer une chaîne de caractères
    std::string result = oss.str();
}



Nep
http://www.cultureg.com
0
adeline11 Messages postés 54 Date d'inscription dimanche 28 septembre 2008 Statut Membre Dernière intervention 18 février 2010 34
24 févr. 2009 à 16:51
le problème était juste qu'il manquait un (std::) devant ma fonction

std:: string Rationnel ....
0