Question

So if I run my program with the implementation as .m it works fine. Just changing it to .mm causes this line…

CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];

to throw this error…

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

Anyone have any ideas why just changing that would make it blow up, or how to fix it?

Was it helpful?

Solution

C++ does not allow implicit type casting from void*. In this case, the implicit converstion from void* (the return type of -[NSGraphicsContext graphicsPort]) a CGContextRef is illegal. You can make the conversion explicit like this:

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

See this SO question for a discussion of the C++ static_cast operator.

OTHER TIPS

C allows converting void * to arbitrary type, C++ does not. Once your file is .mm it is compiled as 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

To fix, cast to proper type, i.e. explicitly cast "void *" to "CGContext *".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top