2012-07-25 79 views
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 
<-- 

(或 “无”,而不是相同的),但我得到

--> 
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); 
+0

如何声明'GeDebugOut'? – jxh 2012-07-25 15:41:36

+0

@ user315052看我的编辑,请见。 – 2012-07-25 15:45:36

+1

是'字符串'的'std :: string' typedef? – jxh 2012-07-25 15:46:54

回答

5

你需要一个像你使用printf使用GeDebugOut

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类型,将传递给+过载。

+0

哦,很高兴知道。但现在应用程序崩溃,我猜是因为它期望'char *'并且我传递了String。但是'String'类可以连接,所以为什么它不能这样工作呢? – 2012-07-25 15:44:50

+0

另请参阅我的编辑,其中包括'GeDebugOut'的声明 – 2012-07-25 15:46:00

+0

@NiklasR:'whatever'应该是一个c-string。 – Nawaz 2012-07-25 15:46:21

1

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

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

一个更好的办法是做:

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

正如你提到的,也有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); 
+0

纠正后编辑注意到编译器不允许在指针变量之间进行算术运算,尽管'String'不太可能转换为数字类型,所以我的怀疑似乎不太有效。 – JohnMcG 2012-07-25 16:12:08