gpt4 book ai didi

c++ - 如何用 C++ 编写这段代码

转载 作者:太空宇宙 更新时间:2023-11-04 15:10:11 27 4
gpt4 key购买 nike

我有以下代码

uint32 joaat_hash(uchar *key, size_t len)
{
uint32 hash = 0;
size_t i;

for (i = 0; i < len; i++)
{
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}

它在 C++ 中的等效代码是什么? i 表示数据类型

1>------ Build started: Project: hash_functions, Configuration: Debug Win32 ------
1> hash_function.cpp
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ';' before identifier 'joaat_hash'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'uchar' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2065: 'key' : undeclared identifier
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2146: syntax error : missing ')' before identifier 'len'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2078: too many initializers
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2275: 'size_t' : illegal use of this type as an expression
1> c:\users\david\documents\visual studio 2010\projects\hash_functions\predefined c++ types (compiler internal)(19) : see declaration of 'size_t'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(3): error C2059: syntax error : ')'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2143: syntax error : missing ';' before '{'
1>c:\users\david\documents\visual studio 2010\projects\hash_functions\hash_function.cpp(4): error C2447: '{' : missing function header (old-style formal list?)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

包括<cstdint> (编译器必须支持 C++0x)并更改以下数据类型:

  • uint32 -> uint32_t (或 std::uint32_t )
  • uchar -> unsigned char

如评论中所述,cstdint 在所有编译器中尚不可用,大多数情况下您可以使用 stdint.h然而(普通的 C99 头文件,不在 std 命名空间中)。

关于c++ - 如何用 C++ 编写这段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353790/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com