5

我正在使用 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
<--

(或者用“no”代替。)但我明白了

-->
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);
4

3 回答 3

5

您需要像使用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类型,以传递给+重载。

于 2012-07-25T15:41:44.290 回答
1

您不能将字符串附加到字符串文字。

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

更好的方法是:

GeDebugOut("Is there a subroot? %s ", str);
于 2012-07-25T15:44:13.533 回答
1

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

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

当它遇到:

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

"Is there a subroot"是一个字符串文字,它转换为 type const char*。我怀疑String有一些数字类型的转换运算符。所以编译器选择了第一个重载。

这会导致您不期望的行为,因为+for 的操作const char*是指针算术,而不是字符串连接,因此您调用GeDebugOut的是字符串文字的指针总和,以及该const char*转换的输出str是什么。

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

GeDebugOut(String("Is there a subroot?") + str);
于 2012-07-25T15:59:37.980 回答