简体   繁体   English

C ++从'void *'到'crypto_aes_ctx *'的无效转换

[英]C++ 'invalid conversion from ‘void*’ to ‘crypto_aes_ctx*’

in order to get a feel for vectors and parallelisation I am currently trying to parallelize my programm with VC ( http://code.compeng.uni-frankfurt.de/projects/vc ). 为了对向量和并行化有所了解,我目前正在尝试将我的程序与VC并行化( http://code.compeng.uni-frankfurt.de/projects/vc )。 My programm is written in C, but VC requires to use C++. 我的程序是用C编写的,但是VC需要使用C ++。 So i renamed my files to .cpp and tried to compile them. 所以我将文件重命名为.cpp并尝试对其进行编译。 I get three compile errors which are all the same 我得到三个相同的编译错误

error: invalid conversion from ‘void*’ to ‘crypto_aes_ctx*’

the code is the following 代码如下

int crypto_aes_set_key(struct crypto_tfm *tfm, const uint8_t *in_key,
    unsigned int key_len)
{
    struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);
    uint32_t *flags = &tfm->crt_flags;
    int ret;
    ret = crypto_aes_expand_key(ctx, in_key, key_len);
    if (!ret)
        return 0;

    *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
    return -EINVAL;
}

how can I fix this to get my code working with a c++ compiler? 如何解决此问题,以使我的代码与C ++编译器一起工作?

Typing in C++ is stricter than C, so you have to use casting to tell the compiler what the void pointer actually is. 在C ++中键入比C严格,因此必须使用强制转换来告诉编译器void指针实际上是什么。

struct crypto_aes_ctx *ctx = (struct crypto_aes_ctx*) crypto_tfm_ctx(tfm);

Note that I'm using a C-style cast, in case you want to continue with the code in C. For C++ you would otherwise use reinterpret_cast . 请注意,如果您要继续使用C中的代码,我正在使用C样式reinterpret_cast 。否则,对于C ++,您应该使用reinterpret_cast

In C++ you may not assign a pointer of type void * to any other pointer of other type without explicit reinterpret casting. 在C ++中,如果没有显式重新解释转换,则不得将void *类型的指针分配给其他任何类型的指针。

If the error occured in statement 如果错误发生在语句中

struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);

then you have to write 那你得写

struct crypto_aes_ctx *ctx = reinterpret_cast<struct crypto_aes_ctx *>( crypto_tfm_ctx(tfm) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM