Rechercher : dans
Par :

Convertir un entier en string

Dernière réponse le 24 fév 2009 à 16:51:26 adeline11, le 6 fév 2009 à 19:06:08 
 Signaler ce message aux modérateurs

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

Configuration: Linux
Firefox 3.0.5

Meilleures réponses pour « Convertir un entier en string » dans :
VB6/VBA Transformer nombre en texte VoirIntroduction Préliminaires VB6 Dans le module de la forme Dans Module1 Introduction La fonction peut transformer des nombres de l'unité jusqu'à 999 tera. Prend en compte la syntaxe pour le français de France, de Belgique et de...
Conversion d'un nombre entier 32 bits en IP VoirConversion d'un nombre entier 32 bits en IP Nombre à convertir : 3265917058 Représentation binaire 11000010 10101001 11110000 10000010 - 3265917058 00000000 00000000 00000000 11000010 - 3265917058 >> 24 ( 194 ) 11000010 10101001 11110000...
[Audio] conversion .RAM, .RM, et .RA en .WAV VoirVoici une solution gratuite et simple d'emploi pour convertir les fichiers .RAM, .RM et .RA en .WMV. STREAMBOX RIPPER (voir ici par exemple : http://qatsi.free.fr/coulisse/StreamboxRipper/StreamboxRipper.html) Remarques : Streambox...
Structure d'un document HTML VoirNotion de document HTML Une page HTML est un simple fichier contenant du texte formatté avec des balises HTML. Par convention l'extension donnée au fichier est .htm ou .html, mais une page web peut potentiellement porter n'importe quelle...
Structures conditionnelles du langage Pascal VoirLes structures de boucle Notre exemple avance. Maintenant, si nous désirons construire une phrase non plus avec trois mots, mais avec cinq, nous n'allons tout de même pas répéter notre code cinq fois !!! Pour cela, il suffit d'utiliser une des...

1

mohamed-hedi, le 6 fév 2009 à 19:11:07

C koi le code de ta fonction rationnel()
ca peu aider :)

Répondre à mohamed-hedi

2

adeline11, le 6 fév 2009 à 19:14:06

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

Répondre à adeline11

3

Nep_51, le 6 fév 2009 à 19:45:58

Bonjour,

Va faire un tour sur ce site tu trouvera peut-être ton bonheur:
http://cpp.developpez.com/faq/cpp/?page=strings#STRINGS_numt­ostr


#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://cultureg.org Vous avez des connaissances? Venez les partager

Répondre à Nep_51

4

 adeline11, le 24 fév 2009 à 16:51:26

Le problème était juste qu'il manquait un (std::) devant ma fonction

std:: string Rationnel ....

Répondre à adeline11