Domanda

Sto usando Visual C ++ per compilare il mio plug-in per CINEMA 4D.

    GeDebugOut("-->");
    subroot = NULL;
    head = NULL;
    tail = NULL;
    success = PolygonizeHierarchy(source, hh, head, tail, &subroot, malloc);
    if (!success) {
        /* .. */
    }
    String str("not set.");
    if (subroot) {
        GeDebugOut("yes");
        str = "yes!";
        GeDebugOut("Subroot name: " + subroot->GetName());
    }
    else {
        GeDebugOut("no");
        str = "no!";
    }
    GeDebugOut("Is there a subroot?   " + str);
    GeDebugOut("<--");
.

L'output previsto è il seguente:

-->
yes
Subroot name: Cube
Is there a subroot?  yes
<--
.

(o lo stesso con "No" invece.) Ma ottengo

-->
yes
<--
.



Perché sono mancanti due stampe qui?


.

Questa è la dichiarazione di GeDebugOut.

void GeDebugOut(const CHAR* s,  ...);
void GeDebugOut(const String& s);
.

La classe String è concatenabile.Sovraccarichi l'operatore +.

String(void);
String(const String& cs);
String(const UWORD* s);
String(const CHAR* cstr, STRINGENCODING type = STRINGENCODING_XBIT);
String(LONG count, UWORD fillch);
friend const String operator +(const String& Str1, const String& Str2);
const String& operator +=(const String& Str);
.

È stato utile?

Soluzione

È necessario utilizzare GeDebugOut come utilizzare printf:

GeDebugOut("Some message =  %s ", whatever);
.

Dove whatever è una stringa C, I.e il suo tipo è char*.

Poiché un sovraccarico di GeDebugOut accetta anche il tipo di generazione di String, quindi penso che sia necessario utilizzare Unicode come:

GeDebugOut(L"Is there a subroot?   " + str);
        // ^ note this!
.

Perché il mio sospetto è che se Unicode è abilitato, allora CHAR è fondamentalmente wchar_t, non char.E a causa di ciò, la concatenazione delle stringhe non funziona, poiché la stringa-letterale non viene implicitamente convertita in tipo String, da passare al sovraccarico +.

Altri suggerimenti

Non è possibile aggiungere una stringa a una stringa letterale.

"Is there a subroot" è una stringa Letterale e il compilatore vedrà l'uso di esso come puntatore a quello letterale.

Un modo migliore sarebbe fare:

GeDebugOut("Is there a subroot? %s ", str);
.

Come hai detto, ci sono due versioni di GeDebugOut il compilatore può scegliere:

void GeDebugOut(const CHAR* s,  ...);
void GeDebugOut(const String& s);
.

Quando incontra:

GeDebugOut("Is there a subroot?   " + str);
.

"Is there a subroot" è una stringa letterale, che si traduce in tipo const char*.Sospetto che String abbia un operatore di conversione ad un tipo numerico.Quindi il compilatore sceglie il primo sovraccarico.

Questo è il risultato del comportamento che non si aspetta, poiché l'operazione + per const char* è puntatore aritmetico, non concatenazione della stringa, quindi stai chiamando GeDebugOut sulla somma del puntatore della String Letterale, e qualunque sia l'output di quel const char*La conversione di str è.

Ci sono diversi modi in cui puoi correggere questo.Come un altro citato, è possibile modificarlo in sintassi simile a printf.Oppure puoi costringerlo a utilizzare il sovrapposizione String come:

GeDebugOut(String("Is there a subroot?") + str);
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top