Convertion d'une class perso en unsigned char

cs_waza Messages postés 8 Date d'inscription mardi 24 décembre 2002 Statut Membre Dernière intervention 19 décembre 2003 - 18 déc. 2003 à 23:09
hilairenicolas Messages postés 398 Date d'inscription jeudi 30 octobre 2003 Statut Membre Dernière intervention 15 juin 2007 - 19 déc. 2003 à 22:17
Salut moi j'ai un probléme de converstion dune classe perso en unsigned char. vc++ me met :"cannot convert from 'class perso' to 'unsigned char'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called"

Voila le code:
Perso temp;
temp = mod(ch, d, n);
return ((unsigned char)temp);

Donc je me demande ce qu'il faut que je rajoute dans ma class pour permettre la converssion?
Merci d'avance!

8 réponses

hilairenicolas Messages postés 398 Date d'inscription jeudi 30 octobre 2003 Statut Membre Dernière intervention 15 juin 2007 2
19 déc. 2003 à 09:53
Je sais pas trop ce que tu veux faire, mais si tu veux copier le contenu de ta classe dans un tableau d'octet, memcpy devrait aller.

Sinon, il faudrait préciser ce qu'est "Perso"

Nico
0
cs_waza Messages postés 8 Date d'inscription mardi 24 décembre 2002 Statut Membre Dernière intervention 19 décembre 2003
19 déc. 2003 à 18:36
alors ma class perso est une class que j'ai trouver sur le net pour le gestion de grand entier naturel...
0
cs_bouba Messages postés 518 Date d'inscription dimanche 2 décembre 2001 Statut Membre Dernière intervention 10 novembre 2007 3
19 déc. 2003 à 18:39
Hum, je ne vois pas du tout comment le compilateur pourrait-il convertir un type inconnu vers un unsigned char. C'est a toi de réliser une fonction de convertion !!!!
0
hilairenicolas Messages postés 398 Date d'inscription jeudi 30 octobre 2003 Statut Membre Dernière intervention 15 juin 2007 2
19 déc. 2003 à 19:04
c'est quoi que tu veux mettre dans une char ? si c'est la valeur de ton grand entier naturel , il faut effectivement que ca soit toi qui produise la fonction de conversion
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_waza Messages postés 8 Date d'inscription mardi 24 décembre 2002 Statut Membre Dernière intervention 19 décembre 2003
19 déc. 2003 à 20:02
Heu justement le probleme c'est que je sais pas comment faire lol...
0
hilairenicolas Messages postés 398 Date d'inscription jeudi 30 octobre 2003 Statut Membre Dernière intervention 15 juin 2007 2
19 déc. 2003 à 20:06
t'as le source de ta classe perso ?
0
cs_waza Messages postés 8 Date d'inscription mardi 24 décembre 2002 Statut Membre Dernière intervention 19 décembre 2003
19 déc. 2003 à 20:21
Voila c'est celle qui est sur ce site://***************************************************************************************// BigInt.h :// Classe BigInt : gestion de nombres entiers codés sur un nombre élevé de// bits.//---------------------------------------------------------------------------------------// - Les nombres utilisés sont signés (valeur absolue + bit de signe et non pas en// utilisant le complément à 2).// - le codage utilise plusieurs entiers non signés de 32 bits pour la sauvegarde des// données.// - le nombre de bits de codage évolue dynamiquement en fonction de la valeur et des // opérations mathématiques faites dessus.//***************************************************************************************#ifndef AFX_BIGINT_H_INCLUDED_#define AFX_BIGINT_H_INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// entier non signé de 32 bitstypedef unsigned long DWORD;//***************************************************************************************// Classe BigInt.//***************************************************************************************class BigInt {//=======================================================================================// Membres privés.//=======================================================================================private : // tableau des données , nombre de données, signe DWORD* m_TabData; int m_NbData; int m_Sign; // générateur aléatoire static DWORD m_dwRand; // copie, optimisation void Copy(BigInt& val); void Optimize(); // affecte, récupère la taille en bits du nombre void SetSize(int nbBits); int GetSize(); // opérations arithmétiques internes void Add(BigInt& val, bool bCheckSign = true); void Sub(BigInt& val, bool bCheckSign = true); void Mul(BigInt& val, bool bCheckSign = true); BigInt Div(BigInt& val, bool bCheckSign = true); DWORD SimpleDiv(BigInt& num, BigInt& val, BigInt& reste); // comparaison et décalage de bits int Compare(BigInt& val, bool bCheckSign = true); void ShiftBits(int dep); // opérations sur les bits du nombre void AndBits(BigInt& val); void OrBits(BigInt& val); void XorBits(BigInt& val);//=======================================================================================// Membres publics.//=======================================================================================public : // constructeurs, destructeur BigInt(int val = 0); BigInt(char* szVal); BigInt(BigInt& val); ~BigInt(); // fonctions du générateur aléatoire static BigInt Random(int nbBits); static void InitRandom(); // exponentielle modulaire static BigInt ExpMod(BigInt& a, BigInt& b, BigInt& n); // afficha les statistiques des fonctions static void PrintStat(); // récupère une représentation affichable du nombre char* GetStrValue(); // récupère le nombre de bits, de DWORD inline int GetNbBits () {return GetSize();} inline int GetNbDWORD () {return m_NbData;} // affecte, réupères les données void SetData(DWORD tab[], int nb); void GetData(DWORD tab[], int nb); // indique si le nombre est pair ou impair inline bool IsEven() {return (m_TabData[0] & 0x00000001) == 0;} inline bool IsOdd() {return (m_TabData[0] & 0x00000001) == 1;} // affectation en profondeur BigInt& operator=(BigInt& val); // division complète (avec quotient et reste) inline BigInt DivEuclide(BigInt& val) {return Div(val);} //----------------------------------------------------------------------------------- // opérateurs arithmétiques sans modification des opérandes inline BigInt operator+(BigInt& val) {BigInt temp = *this; temp.Add(val); return temp;} inline BigInt operator-(BigInt& val) {BigInt temp = *this; temp.Sub(val); return temp;} inline BigInt operator*(BigInt& val) {BigInt temp = *this; temp.Mul(val); return temp;} inline BigInt operator/(BigInt& val) {BigInt temp = *this; temp.Div(val); return temp;} inline BigInt operator%(BigInt& val) {BigInt temp = *this; return temp.Div(val);} inline BigInt operator+(const int val) {return operator+(BigInt(val));} inline BigInt operator-(const int val) {return operator-(BigInt(val));} inline BigInt operator*(const int val) {return operator*(BigInt(val));} inline BigInt operator/(const int val) {return operator/(BigInt(val));} inline BigInt operator%(const int val) {return operator%(BigInt(val));} //----------------------------------------------------------------------------------- // opérateurs arithmétiques avec modification de l'opérande source inline BigInt& operator+=(BigInt& val) {Add(val); return *this;} inline BigInt& operator-=(BigInt& val) {Sub(val); return *this;} inline BigInt& operator*=(BigInt& val) {Mul(val); return *this;} inline BigInt& operator/=(BigInt& val) {Div(val); return *this;} inline BigInt& operator%=(BigInt& val) {Copy(Div(val)); return *this;} inline BigInt& operator+=(const int val) {return operator+=(BigInt(val));} inline BigInt& operator-=(const int val) {return operator-=(BigInt(val));} inline BigInt& operator*=(const int val) {return operator*=(BigInt(val));} inline BigInt& operator/=(const int val) {return operator/=(BigInt(val));} inline BigInt& operator%=(const int val) {return operator%=(BigInt(val));} //----------------------------------------------------------------------------------- // opérateurs d'incrémentation et décrémentation (postfixés et préfixés) inline BigInt& operator++() {Add(BigInt(1)); return *this;} inline BigInt& operator--() {Sub(BigInt(1)); return *this;} inline BigInt operator++(int val) {BigInt temp = *this; Add(BigInt(1)); return temp;} inline BigInt operator--(int val) {BigInt temp = *this; Sub(BigInt(1)); return temp;} //----------------------------------------------------------------------------------- //opérateurs de comparaison inline bool operator== (BigInt& val) {return (Compare(val) == 0);} inline bool operator!= (BigInt& val) {return (Compare(val) != 0);} inline bool operator> (BigInt& val) {return (Compare(val) > 0);} inline bool operator< (BigInt& val) {return (Compare(val) < 0);} inline bool operator>= (BigInt& val) {return (Compare(val) >= 0);} inline bool operator 0);} inline bool operator< (const int val) {return (Compare(BigInt(val)) < 0);} inline bool operator>= (const int val) {return (Compare(BigInt(val)) >= 0);} inline bool operator= 0) { // le signe final est celui de l'appelant int sign = m_Sign; Sub(val, false); m_Sign = sign; } else { // le signe final est celui du nombre de droite BigInt temp = val; temp.Sub(*this, false); Copy(temp); m_Sign = val.m_Sign; } } else { // addition non signé, le signe final est celui du nombre appelant int sign = m_Sign; Add(val, false); m_Sign = sign; } // retour (l'optimisation a déjà été faite)#ifdef USE_STAT TimeAdd += GetTickCount() - deb; NbAdd++;#endif // USE_STAT return; } // récupération des tailles int size1 = GetSize(); int size2 = val.GetSize(); // sauvegarde ancienne valeurs et affectation de la taille du résultat BigInt temp = *this; SetSize(max(size1, size2) + 1); DWORD retenue = 0; for(int i = 0; i < m_NbData; i++) { // premier nombre de la somme partielle unsigned __int64 op1 = 0; if(i < temp.m_NbData) op1 = temp.m_TabData[i]; // second nombre de la somme partielle unsigned __int64 op2 = 0; if(i < val.m_NbData) op2 = val.m_TabData[i]; // résultat partiel et retenue unsigned __int64 result = op1 + op2 + retenue; m_TabData[i] = (DWORD)(result & 0x00000000FFFFFFFF); retenue = (DWORD) (result >> 32); } // optimisation du résultat Optimize();#ifdef USE_STAT TimeAdd += GetTickCount() - deb; NbAdd++;#endif // USE_STAT}//***************************************************************************************// Sub : soustrait un nombre.// entrée : val : le nombre à soustraire.// bCheckSign : vrai s'il faut prendre en compte les signes, faux sinon.//---------------------------------------------------------------------------------------// Remarque : - la taille du nombre est ajustée pour qu'il n'y est pas de dépassement.//***************************************************************************************void BigInt::Sub(BigInt& val, bool bCheckSign){#ifdef USE_STAT DWORD deb = GetTickCount();#endif // USE_STAT // vérif des signes if(bCheckSign) { if(m_Sign != val.m_Sign) { // réalisation d'une addition non signée, le signe final est celui du nombre // appelant int sign = m_Sign; Add(val, false); m_Sign = sign; } else { // réalisation d'une soustraction non signée (le plus grand - le plus petit) if(Compare(val, false) >= 0) { // le signe final est celui de l'appelant int sign = m_Sign; Sub(val, false); m_Sign = sign; } else { // le signe final est l'opposé de celui du nombre de droite BigInt temp = val; temp.Sub(*this, false); Copy(temp); m_Sign = -val.m_Sign; } } // retour (l'optimisation a déjà été faite)#ifdef USE_STAT TimeSub += GetTickCount() - deb; NbSub++;#endif // USE_STAT return; } // récupération des tailles int size1 = GetSize(); int size2 = val.GetSize(); // sauvegarde ancienne valeurs et affectation de la taille du résultat BigInt temp = *this; SetSize(max(size1, size2)); DWORD retenue = 0; for(int i = 0; i < m_NbData; i++) { // premier nombre de la soustraction partielle unsigned __int64 op1 = 0; if(i < temp.m_NbData) op1 = temp.m_TabData[i]; // second nombre de la soustraction partielle unsigned __int64 op2 = 0; if(i < val.m_NbData) op2 = val.m_TabData[i]; if(op1 < op2 + retenue) retenue += (1 = op2 + retenue) { result = op1 - op2 - retenue; retenue = 0; } else { result = op1 + (1 = 0; i--) { reste.ShiftBits(32); reste.m_TabData[0] = temp.m_TabData[i]; if(reste.Compare(val, false) == -1) continue; DWORD quotient = SimpleDiv(reste, val, reste); ShiftBits(32); m_TabData[0] = quotient; } reste.Optimize(); Optimize(); // signe du résultat if(bCheckSign) { reste.m_Sign = sign; m_Sign = sign * val.m_Sign; } // on retourne le reste#ifdef USE_STAT TimeDiv += GetTickCount() - deb; NbDiv++;#endif // USE_STAT return reste;} //***************************************************************************************// Compare : compare avec un nombre.// entrée : val : nombre auquel comparer.// bCheckSign : vrai s'il faut prendre en compte les signes, faux sinon.// retour : -1 si le nombre passé en paramètre est plus grand.// 0 si le nombre passé en paramètre est égal.// +1 le nombre passé en paramètre est plus petit.//***************************************************************************************int BigInt::Compare(BigInt& val, bool bCheckSign){#ifdef USE_STAT DWORD deb = GetTickCount();#endif // USE_STAT // vérif des signes if(bCheckSign) { if(m_Sign != val.m_Sign) {#ifdef USE_STAT TimeComp += GetTickCount() - deb; NbComp++;#endif // USE_STAT return ((m_Sign > val.m_Sign) ? +1 : -1); } } // facteur de sign int sign = 1; if(bCheckSign) sign = m_Sign; // on commence par comparer les tailles int size1 = GetSize(); int size2 = val.GetSize(); if(size1 != size2) {#ifdef USE_STAT TimeComp += GetTickCount() - deb; NbComp++;#endif // USE_STAT return ((size1 > size2) ? +sign : -sign); } // on compare les données à partir du poids fort, et on s'arrête dès qu'elles diffèrent for(int i = m_NbData - 1; i >= 0; i--) { if(m_TabData[i] != val.m_TabData[i]) {#ifdef USE_STAT TimeComp += GetTickCount() - deb; NbComp++;#endif // USE_STAT return ((m_TabData[i] > val.m_TabData[i]) ? +sign : -sign); } } // les 2 nombres sont égaux#ifdef USE_STAT TimeComp += GetTickCount() - deb; NbComp++;#endif // USE_STAT return 0;}//***************************************************************************************// Optimize : optimise le nombre de bits de codage en supprimant les bits de poids fort// qui sont à 0.//***************************************************************************************void BigInt::Optimize(){#ifdef USE_STAT DWORD deb = GetTickCount();#endif // USE_STAT // nombre de données réellement utiles int nbNewData = m_NbData; while(nbNewData > 1 && m_TabData[nbNewData - 1] == 0) nbNewData--; // si toutes les données sont utiles if(nbNewData == m_NbData) {#ifdef USE_STAT TimeOpt += GetTickCount() - deb; NbOpt++;#endif // USE_STAT return; } // ne récupérer que les données utiles m_NbData = nbNewData; DWORD* tabNewData = new DWORD[m_NbData]; for(int i = 0; i < m_NbData; i++) tabNewData[i] = m_TabData[i]; if(m_NbData == 0 && m_TabData[0] == 0) m_Sign = 1; // détruire l'ancien tableau et affecter le nouveau delete[] m_TabData; m_TabData = tabNewData;#ifdef USE_STAT TimeOpt += GetTickCount() - deb; NbOpt++;#endif // USE_STAT}//***************************************************************************************// ShiftBits : effectue un déplacement des bits.// entrée : dep : le déplacement à effectuer :// - >0 pour un déplacement à gauche.// - 0) { // sauvegarder le nombre et modifier la taille du tableau des données BigInt temp = *this; temp.Optimize(); SetSize(temp.GetSize() + dep); // nombre de données entière à décaler (on le fait même si nb est nul pour recopier // le tableau de données int nb = dep / 32; for(int i = temp.m_NbData - 1; i >= 0; i--) m_TabData[i + nb] = temp.m_TabData[i]; // décalages des derniers bits (on utilise des entiers de 64 bits pour ne perdre // aucune données, sauf pour le poids fort) nb = dep % 32; if(nb != 0) { m_TabData[m_NbData - 1] nb; m_TabData[i - 1] |= (val & 0x00000000FFFFFFFF); m_TabData[i] >>= nb; } } } // optimisation de la taille des nombres if(dep != 0) Optimize();#ifdef USE_STAT TimeShift += GetTickCount() - deb; NbShift++;#endif // USE_STAT}//***************************************************************************************// AndBits : effectue un ET des bits.// entrée : val : nombre avec lequel réaliser l'opération//---------------------------------------------------------------------------------------// Remarque : la taille du nombre est ajustée pour qu'il n'y est pas de dépassement.//***************************************************************************************void BigInt::AndBits(BigInt& val){ // récupération des tailles int size1 = GetSize(); int size2 = val.GetSize(); // sauvegarde ancienne valeurs et modif de la taille if(size1 < size2) { BigInt temp = *this; SetSize(size2); memcpy(m_TabData, temp.m_TabData, temp.m_NbData * sizeof(DWORD)); } // on effectue le AND bit à bit for(int i = 0; i < m_NbData; i++) { if(i < val.m_NbData) m_TabData[i] &= val.m_TabData[i]; else m_TabData[i] = 0; } // optimisation du résultat Optimize();}//***************************************************************************************// OrBits : effectue un OU des bits.// entrée : val : nombre avec lequel réaliser l'opération//---------------------------------------------------------------------------------------// Remarque : la taille du nombre est ajustée pour qu'il n'y est pas de dépassement.//***************************************************************************************void BigInt::OrBits(BigInt& val){ // récupération des tailles int size1 = GetSize(); int size2 = val.GetSize(); // sauvegarde ancienne valeurs et modif de la taille if(size1 < size2) { BigInt temp = *this; SetSize(size2); memcpy(m_TabData, temp.m_TabData, temp.m_NbData * sizeof(DWORD)); } // on effectue le OR bit à bit for(int i = 0; i < val.m_NbData; i++) m_TabData[i] |= val.m_TabData[i]; // optimisation du résultat Optimize();}//***************************************************************************************// XorBits : effectue un OU exclusif des bits.// entrée : val : nombre avec lequel réaliser l'opération//---------------------------------------------------------------------------------------// Remarque : la taille du nombre est ajustée pour qu'il n'y est pas de dépassement.//***************************************************************************************void BigInt::XorBits(BigInt& val){ // récupération des tailles int size1 = GetSize(); int size2 = val.GetSize(); // sauvegarde ancienne valeurs et modif de la taille if(size1 < size2) { BigInt temp = *this; SetSize(size2); memcpy(m_TabData, temp.m_TabData, temp.m_NbData * sizeof(DWORD)); } // on effectue le XOR bit à bit for(int i = 0; i < val.m_NbData; i++) m_TabData[i] ^= val.m_TabData[i]; // optimisation du résultat Optimize();}//***************************************************************************************// Affectation en profondeur.// entrée : val : nombre à recopier.// retour : référence sur l'objet appelant (pour pouvoir faire a=b=c).//***************************************************************************************BigInt& BigInt::operator=(BigInt& val){ // précaution en cas de a=a if(this == &val) return *this; // destruction de l'ancien tableau et copie des nouvelles données delete[] m_TabData; Copy(val); // retour return *this;}
0
hilairenicolas Messages postés 398 Date d'inscription jeudi 30 octobre 2003 Statut Membre Dernière intervention 15 juin 2007 2
19 déc. 2003 à 22:17
dans le .h je vois cette fonction, ca suffit pas ?

// récupère une représentation affichable du nombre
char* GetStrValue();
0
Rejoignez-nous