为什么我的部分代码没有被执行?

Nik*_*s R 5 c++ compiler-optimization cinema-4d

我正在使用Visual C++来编译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("<--");
Run Code Online (Sandbox Code Playgroud)

预期的输出如下:

-->
yes
Subroot name: Cube
Is there a subroot?  yes
<--
Run Code Online (Sandbox Code Playgroud)

(或者与"不"相反.)但是我得到了

-->
yes
<--
Run Code Online (Sandbox Code Playgroud)


为什么这里缺少两张照片?


这是宣言GeDebugOut.

void GeDebugOut(const CHAR* s,  ...);
void GeDebugOut(const String& s);
Run Code Online (Sandbox Code Playgroud)

String班是concatenateable.它使+操作员超载.

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);
Run Code Online (Sandbox Code Playgroud)

Naw*_*waz 5

您需要GeDebugOut像使用一样使用printf:

GeDebugOut("Some message =  %s ", whatever);
Run Code Online (Sandbox Code Playgroud)

where whatever是一个c-string,即它的类型是char*.

既然GeDebugOut接受String类型的重载也是,那么我认为你需要使用unicode:

GeDebugOut(L"Is there a subroot?   " + str);
        // ^ note this!
Run Code Online (Sandbox Code Playgroud)

因为我的怀疑是如果启用了unicode,那么CHAR基本上wchar_t不是char.因此,字符串连接不起作用,因为字符串文字不会隐式转换为String类型,而是传递给+重载.

  • 你有GeDebugOut()接受`char*`,当你传递它的字符串文字,它选择那个,所以你的`+`正在做指针算术而不是字符串连接,特别是如果`String`转换为`char*`. (2认同)