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

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

本文关键字:无效 ctx 转换 aes crypto C++      更新时间:2023-10-16

为了了解矢量和并行化,我目前正在尝试用VC并行化我的程序(http://code.compeng.uni-frankfurt.de/projects/vc)。我的程序是用C编写的,但VC要求使用C++。因此,我将文件重命名为.cpp,并尝试编译它们。我得到三个编译错误,它们都是相同的

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

代码是以下

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;
}

如何修复此问题以使我的代码与c++编译器一起工作?

C++中的键入比C更严格,因此必须使用强制转换来告诉编译器void指针实际是什么。

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

请注意,我使用的是C样式转换,以防您想继续使用C中的代码。对于C++,您将使用reinterpret_cast

在C++中,如果没有显式的重新解释强制转换,则不能将类型为void *的指针分配给任何其他类型的指针。

如果语句中出现错误

struct crypto_aes_ctx *ctx = crypto_tfm_ctx(tfm);

然后你必须写

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