Xcode';无效转换';使用objective-c++;

Xcode';无效转换';使用objective-c++;,xcode,macos,Xcode,Macos,所以,如果我以.m的形式运行我的程序,它就可以正常工作。只是将其更改为.mm会导致这行 CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; 要抛出此错误 error: invalid conversion from 'void*' to 'CGContext*' 谁有任何想法,为什么只改变它会使它爆炸,或者如何修复它? < P> C允许转换空洞*为任意类型,C++不。 一旦文件为.mm,它将编

所以,如果我以.m的形式运行我的程序,它就可以正常工作。只是将其更改为.mm会导致这行

CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
要抛出此错误

error: invalid conversion from 'void*' to 'CGContext*'

谁有任何想法,为什么只改变它会使它爆炸,或者如何修复它?

< P> C允许转换空洞*为任意类型,C++不。 一旦文件为.mm,它将编译为C++:

cristi:tmp diciu$ cat test.c
int main()
{
    char * t = "test";
    void * m = t;
    char * g;

    g=m;

}

cristi:tmp diciu$ g++ test.c
test.c: In function ‘int main()’:
test.c:7: error: invalid conversion from ‘void*’ to ‘char*’

cristi:tmp diciu$ gcc test.c

要修复,请强制转换为正确的类型,即显式地将“void*”强制转换为“CGContext*”

C++不允许从
void*
进行隐式类型转换。在这种情况下,来自
void*
(返回类型为
-[NSGraphicsContext graphicsPort]
)a
CGContextRef
的隐式对话是非法的。您可以像这样显式地进行转换:

CGContextRef myContext = static_cast<CGContextRef>([[NSGraphicsContext currentContext] graphicsPort]);
CGContextRef myContext=static_cast([[NSGraphicsContext currentContext]graphicsPort]);

参阅问题,讨论C++ <代码>静态分析> <代码>操作符> < /p>