C++ 中的 C 代码编译器
我有以下代码,它是来自 tomcrypto 手册的代码,它不适用于 MS VC++ 2008 EE。有什么帮助吗?另外我可以要求用 std::string
对象替换 char*
吗?
int main(void)
{
hash_state md;
unsigned char *in = "hello world", out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
错误:
\main.cpp(7) : error C2440: 'initializing' : cannot convert from 'const char [12]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(11) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
编辑:现在代码看起来像:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
char* p = "hello wordl";
unsigned char *in = reinterpret_cast<unsigned char*>(p);
char* out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, const_cast<char*>(in), strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
错误:
\main.cpp(21) : error C2440: 'const_cast' : cannot convert from 'unsigned char *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(21) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(23) : error C2664: 'md5_done' : cannot convert parameter 2 from 'char *[16]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
I have following code, it's code from tomcrypto's manual and it won't work on MS VC++ 2008 EE. Any help? Also can I ask replace char*
by std::string
object?
int main(void)
{
hash_state md;
unsigned char *in = "hello world", out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(7) : error C2440: 'initializing' : cannot convert from 'const char [12]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(11) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
EDIT: Now code looks like:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
char* p = "hello wordl";
unsigned char *in = reinterpret_cast<unsigned char*>(p);
char* out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, const_cast<char*>(in), strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(21) : error C2440: 'const_cast' : cannot convert from 'unsigned char *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(21) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(23) : error C2664: 'md5_done' : cannot convert parameter 2 from 'char *[16]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这在 C++ 中是不正确的:
"hello world"
是一个字符串文字,并且类型为const char[12]
。在 C 中,它的类型为char[12]
,但这里的const
并不重要,因为在 C++ 中,存在允许字符串文字的隐式(但已弃用)转换转换为char*
。问题是
char
和unsigned char
是不同的类型。char
是否无符号并不重要;三种char
类型(char
、unsigned char
和signed char
)都是不同的,在 C++ 中你不能无需强制转换即可在指向这三种类型的指针之间进行转换。这在 C 中有效,因为在 C 中,您可以将任何指向对象的指针类型转换为任何其他指向对象的指针类型,而无需强制转换。 C++ 中的情况并非如此。
在 C++ 中,您需要使用:
删除常量通常是一个坏主意,因为字符串文字不可修改,但有时在处理不常量正确的遗留库时有必要。
从
char*
到unsigned char*
的转换是安全的,因为所有对象都可以被视为char
、unsigned char< /code>,或 C++ 中的
signed char
。This is incorrect in C++:
"hello world"
is a string literal and is of typeconst char[12]
. In C it is of typechar[12]
, but theconst
here doesn't matter because in C++ there is an implicit (but deprecated) conversion that allows a string literal to be converted to achar*
.The problem is that
char
andunsigned char
are different types. It doesn't matter whetherchar
is unsigned; the threechar
types (char
,unsigned char
, andsigned char
) are all distinct and in C++ you cannot convert between pointers to those three types without a cast.This works in C because in C you can convert any pointer-to-object type to any other pointer-to-object type without a cast. That isn't the case in C++.
In C++ you would need to use:
The removal of constness is usually a bad idea since string literals are not modifiable, but sometimes it is necessary when dealing with legacy libraries that are not const-correct.
The conversion from
char*
tounsigned char*
is safe because all objects can be treated as an array ofchar
,unsigned char
, orsigned char
in C++.char
是与signed char
或unsigned char
不同的类型;字符串文字始终为(const) char *
类型;因此您不能将它们分配给(const)signed char *
或(const)unsigned char*
。要解决此问题,请从第 4 行删除unsigned
。如果您的
md5_process()
函数明确采用unsigned char *
作为参数,那么您应该在此时执行强制转换:[正如其他人所说,您应该将
in
定义为const char *in
因为它指向字符串文字,但是这不是这里的问题。]char
is a different type tosigned char
orunsigned char
; string literals are always of type(const) char *
; so you cannot assign them to a(const) signed char *
or a(const) unsigned char *
. To fix this, remove theunsigned
from line 4.If your
md5_process()
function explicitly takes anunsigned char *
as an argument, then you should perform a cast at that point:[As others have said, you should really define
in
asconst char *in
as it's pointing to a string literal, but that is not the issue here.]让我们再试一次:
这有效吗?
Let's try again:
Does this work?
这是因为在 C++ 中,文字字符串是 const,而您使用非常量指针初始化它:
但是,如果 md5_process 采用非常量
char*
,则可能会导致问题,在这种情况下,您将必须转换为非常量:This is because litteral strings are const in C++, while you initialize it with a non-const pointer:
However it might cause a problem if md5_process takes a non-const
char*
, in this case you'll have to cast to a non-const: