Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

pkoi ca marche pas

5 réponses
Avatar
yeye yaya
Hello,
Why doesn't this work? it says

" error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called "

And is there a solution?

//Code starts here

void CDay2Dlg::OnRunpgm()
{
UpdateData(true);
CString strPgmName;
strPgmName = m_strProgToRun;
strPgmName.MakeUpper();

switch(strPgmName) {
case "PAINT":
WinExec("pbrush.exe",SW_SHOW);
break;
case "NOTEPAD":
WinExec("notepad.exe",SW_SHOW);
break;
case "SOLITAIRE":
WinExec ("sol.exe",SW_SHOW);
break;
}

}

5 réponses

Avatar
Alain Naigeon
"yeye yaya" a écrit dans le message news:
ben199$uie$
Hello,
Why doesn't this work? it says

" error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called "

And is there a solution?

//Code starts here

void CDay2Dlg::OnRunpgm()
{
UpdateData(true);
CString strPgmName;
strPgmName = m_strProgToRun;
strPgmName.MakeUpper();

switch(strPgmName) {
case "PAINT":


...

1) "pkoi" s'écrit "pourquoi" - sinon le compilateur de Français râle.

2) l'expression testée par switch doit être un type entier ou
énuméré ; ce peut être une classe, mais évidemment à
la condition qu'elle ait un opérateur de conversion vers
l'un des deux types précédents.

3) il faut croire que la classe CString (propriétaire MS je crois),
n'a pas un tel opérateur - d'ailleurs on voit mal ce qu'il
pourrait être.

4) il faut donc remplacer ton switch par une collection de
tests ordinaires par if...

--

Français *==> "Musique renaissance" <==* English
midi - facsimiles - ligatures - mensuration
http://anaigeon.free.fr | http://www.medieval.org/emfaq/anaigeon/
Alain Naigeon - - Strasbourg, France

Avatar
Luc Hermitte
Salut.


Tout d'abord, permets moi de te rapeller que "fr" dans
"fr.comp.lang.c++" implique "en langue *FR*ançaise" -- pourtant, ton
sujet est bien en langue française d'ailleurs.

"yeye yaya" wrote in news:ben199$uie$
reader8.wanadoo.fr:

void CDay2Dlg::OnRunpgm()
{
[...]
CString strPgmName = m_strProgToRun;
[...]

switch(strPgmName) {
[...]
}
}

Why doesn't this work? it says
" error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called "


C'est pourtant clair. Un switch ne peut se faire sur un type de classe
CString.
En fait, un switch ne peut se faire _que_ sur des types énumérables
(entiers et dérivés).

Tu vas donc devoir employer au choix :
- des
if(...) {...}
elseif (...) {...}
elseif (...) {...}
...
else {...}

Traduction classique du switch ...

- des std::maps
typedef std::map<std:string, std::string> table;
table table_assoc;
table_assoc["Paint"] = "pbrush.exe";
...
...
table::const_iterator ch =
find(table_assoc.begin(), table_assoc.end(), laChaineCherchee);
if (table_assoc.end() != ch)
WinExec(ch.first.c_str(), ch.second.str(), SW_SHOW);
else
std::err << "le programme " << laChaineCherchee
<< "n'est pas référencé" << std::endl;

Avantage : peut-etre étendu sans recompilation


Il y a encore d'autres façon de faire la même chose. Façon plus ou moins
souples/complexes/évolutives/...


--
Luc Hermitte <hermitte at free.fr>
FAQ de <news:fr.comp.lang.c++> :
<http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ/>
Dejanews : <http://groups.google.com/advanced_group_search>

Avatar
yeye yaya
Je suis désolé pour mon français et pour la partie en anglais du message.

Je viens de Delphi pour découvrir et migrer vers c++ et je rencontre de
serieux problèmes pour travailler avec du texte (chose evidente sous
delphi), toute aide sera la bienvenue.

Merci
Avatar
Vincent Richard

Je viens de Delphi pour découvrir et migrer vers c++ et je rencontre de
serieux problèmes pour travailler avec du texte (chose evidente sous
delphi), toute aide sera la bienvenue.


Un bon conseil : utiliser tous les outils fournis par la bibliothèque
standard (STL) : "std::vector" pour les tableaux, "std::string" pour
manipuler les chaînes de caractères, etc...

Vincent

--
SL> Au fait elle est mieux ma signature maintenant ?
Oui. T'enlève encore les conneries que t'as écrit dedans et c'est bon.
-+- JB in <http://www.le-gnu.net> : Le neuneuttoyage par le vide -+-

Avatar
Emmanuel Delahaye
In 'fr.comp.lang.c++', Luc Hermitte wrote:

Tu vas donc devoir employer au choix :
- des
if(...) {...}
elseif (...) {...}


else if (...) {...}

--
-ed- [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/