C++ 错误:从“void*”到“char*”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18879344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:17:14  来源:igfitidea点击:

C++ error: invalid conversion from 'void*' to 'char*'

c++

提问by user2792662

I have this C++ code:

我有这个 C++ 代码:

#include <stdlib.h>
int main(){
    char *Teclas;
    Teclas = calloc(1024,sizeof(char));
}

And the compiler is giving the following error:

并且编译器给出以下错误:

error: invalid conversion from `void*' to `char*'

What does this error mean and how do I fix it?

这个错误是什么意思,我该如何解决?

回答by Mike Seymour

The problem is that you're trying to compile C with a C++ compiler. As the error message says, this line:

问题是您正在尝试使用 C++ 编译器编译 C。正如错误消息所说,这一行:

Teclas = calloc(1024,sizeof(char));

tries to convert the untyped void*pointer returned by callocinto a typed char*pointer to assign to the variable of that type. Such a conversion is valid in C, but not C++.

尝试将void*返回的无类型指针转换calloc为有类型char*指针以分配给该类型的变量。这种转换在 C 中有效,但在 C++ 中无效。

The solution is to use a C compiler. It looks like you're using GCC, so just rename the source file to something.c, and build with gccrather than g++.

解决方案是使用 C 编译器。看起来您正在使用 GCC,因此只需将源文件重命名为something.c,并使用gcc而不是g++.

If you really must use a compiler for the wrong language, and don't feel like rewriting this in idiomatic C++, then you'll need a cast to force it through the compiler:

如果你真的必须为错误的语言使用编译器,并且不想用惯用的 C++ 重写它,那么你需要一个强制转换来强制它通过编译器:

Teclas = static_cast<char*>(calloc(1024,sizeof(char)));

or, if you want the code to remain valid C:

或者,如果您希望代码保持有效 C:

Teclas = (char*)calloc(1024,sizeof(char));

But don't do that: use the right compiler for the language. Unless this is the first stage in converting the program to C++; in which case, the next thing to do is get rid of these allocations and use std::stringinstead.

但不要那样做:为语言使用正确的编译器。除非这是将程序转换为 C++ 的第一阶段;在这种情况下,接下来要做的是摆脱这些分配并使用std::string

回答by Mike Seymour

void int main(int argc,char *argv[])

uhm... perhaps just int main(int argc, char *argv[])...

嗯……也许只是int main(int argc, char *argv[])……

Apart from that: this looks like C code. Nothing in these lines suggests that you use C++. The error you are seeing is the result of you treating C code as if it was C++, whereas it isn't, because C is not C++, C++ is not C, and neither is the subset of the other one.

除此之外:这看起来像 C 代码。这些行中没有任何内容表明您使用 C++。您看到的错误是您将 C 代码视为 C++ 的结果,而事实并非如此,因为 C 不是 C++,C++ 不是 C,也不是另一个的子集。



Compile your C code with a C compiler.

使用 C 编译器编译 C 代码。

回答by Andy

calloc()returns a void*. You need to cast its value to whatever type Teclas is, which appears to be a char*. So Teclas = (char*)calloc(...).

calloc()返回一个空*。您需要将其值转换为 Teclas 的任何类型,这似乎是一个char*. 所以Teclas = (char*)calloc(...)

回答by C0deH4cker

The best solution is to just compile your code with a C compiler, as this is C code. You really shouldn't compile C code as if it were C++. However, to directly answer your question, you can force the C++ compiler to compile your C code (which is very bad!!!!).

最好的解决方案是使用 C 编译器编译您的代码,因为这是 C 代码。您真的不应该像编译 C++ 一样编译 C 代码。但是,要直接回答您的问题,您可以强制 C++ 编译器编译您的 C 代码(这非常糟糕!!!!)。

In C++, whenever you use any function of the allocfamily, you must cast the return value to the type of your lvalue. So, in this case:

在 C++ 中,无论何时使用该alloc系列的任何函数,都必须将返回值强制转换为左值的类型。所以,在这种情况下:

Teclas = (char*)calloc(1024, sizeof(char));

The way in which I would do the above is like this:

我将执行上述操作的方式是这样的:

Teclas = (char*)malloc(1024 * sizeof(*Teclas));

The benefits here: If you change the type of Teclas, you will still end up with the correct allocation size. Also, I just prefer using mallocin general.

这里的好处是:如果您更改 的类型Teclas,您仍然会得到正确的分配大小。另外,我只是更喜欢malloc一般使用。

Additionally, you have major issues throughout your code. For one thing, void int main(...)???? And you never initialize the contents of Teclasbefore you print it, but you free it and calloc it again for some reason. I think you meant to make that a do while loop, but there is no do.

此外,您在整个代码中都存在重大问题。一方面,void int main(...)???并且您Teclas在打印之前永远不会初始化 的内容,但是您出于某种原因将其释放并再次调用它。我认为您打算将其设为 do while 循环,但没有do.

Also, void KeyLogger();is WRONG. That is how you declare the function, but since it is a declaration, it should be outside of main.

另外,void KeyLogger();是错误的。这就是你声明函数的方式,但由于它是一个声明,它应该在 main 之外。

回答by Fred F

On the android NDK JNI, even with typecasting, old style or new style, the error still doesn't go away.

在android NDK JNI上,即使是类型转换,旧样式或新样式,错误仍然没有消失。

buffer = (char *) malloc(10);
xxxx=static_cast<char*>(calloc(1024,sizeof(char)));

To make the errors go away, an extra include needs to be added to the path and symbols.

为了使错误消失,需要在路径和符号中添加一个额外的包含。

Project -> properties -> C/C++ general -> Path and symbols

项目 -> 属性 -> C/C++ 通用 -> 路径和符号

On the Includes tab/GNU C++, add the following path (with the appropriate gcc version 4.6, 4.8...) Of course on windows, the path would be be a windows path....

在 Includes 选项卡/GNU C++ 上,添加以下路径(使用适当的 gcc 版本 4.6、4.8...)当然在 Windows 上,路径将是 Windows 路径....

{NDKROOT}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.6/include

{NDKROOT}/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.6/include

回答by franksai

If you can make sure there is no other error in your code, you can add g++ flag: -fpermissiveto put the error into warning.
exp: g++ -fpermissive yourcode.cpp

如果您可以确保代码中没有其他错误,则可以添加 g++ 标志:-fpermissive将错误放入警告中。
经验:g++ -fpermissive yourcode.cpp