【发布时间】:2018-06-20 10:34:02
【问题描述】:
我正在尝试使用 Microsoft MSVC 2015 64 位编译器将旧项目从 Borland BCC 5 迁移到 Qt 5.6.3。
以下代码为原创作品的模型。它在 Visual Studio 2015 和 Borland BCC 5 中按预期工作,但是,当我尝试使用 Qt Creator 3.6.1 在 Qt 5.6.3 中运行它时。由于“*hcID”不可访问而引发错误。
#include <stdlib.h>
typedef void (*ProcFoo) (void *pvData, unsigned long ulBarTag);
static char* gpcID = NULL;
typedef struct FooType
{
ProcFoo pfFoo; ///< pointer to Foo callback function
unsigned long ulBarTag; ///< base id
} FooType;
FooType* pFoo = NULL;
void RegisterAsFoo(unsigned long ulBarTag, ProcFoo pfFoo)
{
pFoo = (FooType*)malloc(sizeof(FooType));
pFoo->ulBarTag = ulBarTag;
pFoo->pfFoo = pfFoo;
}
void GetBarTag(unsigned long *pulBarTag, unsigned long ulBarTag)
{
*pulBarTag = ulBarTag;
}
int main()
{
char s[] = "Some Value";
gpcID = s;
char ** hcID = NULL;
RegisterAsFoo((unsigned long)&gpcID, (ProcFoo)GetBarTag);
pFoo->pfFoo(&hcID, pFoo->ulBarTag);
// after the execution, the hcID is still NULL in Qt 5.6.3, however,
// it is valid in Visual Studio 2015 and Borland BCC 5.
char* result = (*hcID);
return 1;
}
如果您有任何 cmets 或想法,谢谢!
【问题讨论】:
-
C 和 C++ 是不同的语言。请选择一个。
-
@JonathonReinhart 感谢您的评论。我删除了 C++ 标志。
-
@Risheng 你确定 C 是正确的选择吗? Qt是C++库,C没有
<iostream>或new。 -
现在您的代码被标记为 C 但其中包含 C++ 代码:
pFoo = new FooType;也许第一步是弄清楚您正在使用哪种编程语言。 -
@JonathonReinhart 抱歉,有误会。我用原来的malloc替换了新方法。这是一个非常古老的项目,核心是用纯 C 编写的,还有一些额外的 C++ dll。现在我的团队需要迁移到 Qt 平台,使用 Windows MSVC 编译器。