我正在使用 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("<--");

预期输出如下:

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

(或者与“不”相同。)但我明白

-->
yes
<--


为什么这里缺少两幅印刷品?


这是声明 GeDebugOut.

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

String 类是可串联的。它超载了 + 操作员。

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);
有帮助吗?

解决方案

你需要使用 GeDebugOut 就像你用的一样 printf:

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

在哪里 whatever 是一个c字符串,即它的类型是 char*.

由于超载 GeDebugOut 接受 String 类型也,那么我认为你需要使用 unicode 作为:

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

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

其他提示

您无法将字符串追加到字符串文字中。

"Is there a subroot"是一个字符串文字,编译器将看到它用作指向该文字的指针。

更好的方法是做:

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

正如您提到的,有两个版本 GeDebugOut 编译器可以选择:

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

当它遇到:

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

"Is there a subroot" 是一个字符串文字,它会转换为类型 const char*. 。我猜测 String 有一个到某种数字类型的转换运算符。所以编译器选择第一个重载。

这会导致您意想不到的行为,因为 + 操作为 const char* 是指针算术,而不是字符串连接,所以你正在调用 GeDebugOut 在字符串文字的指针总和上,以及无论其输出是什么 const char* 的转换 str 是。

有几种方法可以纠正这个问题。正如另一位提到的,你可以将其更改为 printf- 类似语法。或者你可以强制它使用 String 像这样重叠:

GeDebugOut(String("Is there a subroot?") + str);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top