Pergunta

Portanto, se eu executar meu programa com a implementação como .m, ele funciona bem. Apenas mudar para .mm causa essa linha ...

CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];

Para apresentar este erro ...

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

Alguém tem alguma idéia, por que apenas mudar isso faria com que explodir ou como corrigi -lo?

Foi útil?

Solução

C ++ não permite fundição de tipo implícita de void*. Nesse caso, a conversão implícita de void* (o tipo de retorno de -[NSGraphicsContext graphicsPort]) uma CGContextRef é ilegal. Você pode tornar a conversão explícita assim:

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

Ver isto Então, pergunta para uma discussão sobre o C ++ static_cast operador.

Outras dicas

C permite a conversão do vazio * para o tipo arbitrário, o C ++ não. Depois que seu arquivo é .mm, ele é compilado como 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

Para consertar, fundido para o tipo adequado, ou seja, lançar explicitamente "void *" para "CGContext *".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top