C++两个不同函数签名的重定义错误
C++ redefinition error for two different function signatures
我有一个类型,morton_id_t
,这是一个64位的数字。我有第二种类型,它是Morton ID (morton_id_t
)的位与包含树中级别信息的几个位的串联:
typedef uint64_t morton_id_t;
typedef uint64_t morton_id_lvl_t;
#define BITS (sizeof(morton_id_lvl_t) * 8)
// number of bits to store the level
#define LEVEL_BITS 6
#define LEVEL_MASK ((1 << LEVEL_BITS) - 1)
// utility functions to pull the level or morton id out of a morton_id_lvl_t
lvl_t level(morton_id_lvl_t n);
morton_id_t morton_id(morton_id_lvl_t n);
// this just strips off the level bits using the `morton_id` function...
bool isAncestor(morton_id_lvl_t ancestor, morton_id_lvl_t descendent);
// ...and calls this
bool isAncestor(morton_id_t ancestor, morton_id_t descendent);
morton_id_t
只是从uint64_t
typedef
,即使我只需要 64 - 6 级位。
编译器抛出一个错误,指出第二个isAncestor
定义重新定义了第一个定义,即使类型签名不同。
如何定义两个不同的isAncestor
函数,其中一个取morton_id_lvl_t
,另一个取morton_id_t
?
我需要使用类而不是typedef
类吗?
morton_id_t
和morton_id_lvl_t
是相同的,因此isAncestor
签名也相同。
typedef
不会创建新类型,而是创建现有类型的别名。
不幸的是,typedef 只为类型引入了一个新名称,它不会创建新类型。
您可以将它们包装在结构中以将它们转换为不同的类型:
struct morton_id_t
{
uint64_t id;
};
struct morton_id_lvl_t
{
uint64_t id_lvl;
};
或者可能(不是 100% 确定它是有效的):
struct morton_id_lvl_t
{
uint64_t id:58;
uint64_t level:6;
};
这也将为转换函数提供类型安全性。
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 使用命名空间时出现多个定义错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 在运算符重载定义中使用成员函数(const错误)
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在C++中使用内联方法时出现未定义的符号错误
- 已定义函数时出现 G++ "未定义的引用"错误
- C++ G++ 编译器 - 错误:隐式声明的定义
- 链接 cmake 时出现未定义的引用错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接阶段出现多重定义错误
- 已经以性格错误定义了
- vim ctag导致了对乐趣的错误定义
- C++ 隐式声明的编译器错误定义
- 编译器错误-定义问题.C++