gpt4 book ai didi

c++ - 不明确的重载函数 - 如何以及为什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:51 25 4
gpt4 key购买 nike

我很难弄清楚是什么让某些函数调用变得模棱两可,而其他函数调用却很好。我的 BitPacker 对象中有以下重载函数调用:

static __declspec(dllexport) void PackBits(void *dstBuffer, unsigned long long data, unsigned int &offsetBits, const unsigned int numBits);
static __declspec(dllexport) void PackBits(void *dstBuffer, bool data, unsigned int &offsetBits, const unsigned int numBits);
static __declspec(dllexport) void PackBits(void *dstBuffer, float data, unsigned int &offsetBits, const unsigned int numBits);
static __declspec(dllexport) void PackBits(void *dstBuffer, double data, unsigned int &offsetBits, const unsigned int numBits);

我正在尝试从另一个包含“BitPacker.h”的对象中进行以下调用:

void Date::ReflectToBitBuffer(void)
{
unsigned int offsetBits = 0;
BitPacker::PackBits(m_packedBuffer, m_year, offsetBits, YR_BITS);
BitPacker::PackBits(m_packedBuffer, m_month, offsetBits, MO_BITS);
BitPacker::PackBits(m_packedBuffer, m_day, offsetBits, DY_BITS);
}

“m_year”、“m_month”和“m_day”是 int 成员变量。但是我在尝试编译时遇到以下错误:

error C2668: 'BitPacker::PackBits' : ambiguous call to overloaded function
could be 'void BitPacker::PackBits(void *,double,unsigned int &,const unsigned int)'
or 'void BitPacker::PackBits(void *,float,unsigned int &,const unsigned int)'
or 'void BitPacker::PackBits(void *,bool,unsigned int &,const unsigned int)'
or 'void BitPacker::PackBits(void *,unsigned __int64,unsigned int &,const unsigned int)'
while trying to match the argument list '(char *, int, unsigned int, )'

所以我用下面的 main.cpp 编写了一个测试解决方案来测试过载,下面的内容编译得很好:

void OverloadTest(float f);
void OverloadTest(int n, int &numbits);
void OverloadTest(double d);
void OverloadTest(char c, int &numbits);
void OverloadTest(long long l, int &numbits);
void OverloadTest(long long *l);
void OverloadTest(bool b);

int main(int argc, int *argv)
{
int myInt = 77;
bool myBool = true;
float myFloat = 3.14159f;
double myDouble = 1.57;
long long myLongLong = 12345;
long long *ptrLongLong = NULL;
char myChar = 'q';
int myNumBits = 10;

OverloadTest(myInt, myNumBits);
OverloadTest(myFloat);
OverloadTest(myDouble);
OverloadTest(myLongLong, myNumBits);
OverloadTest(ptrLongLong);
OverloadTest(myChar, myNumBits);
OverloadTest(myBool);

return 0;
}

void OverloadTest(float _f) { int x = 0; }
void OverloadTest(int _n, int &_numbits) { int x = 0; }
void OverloadTest(double _d) { int x = 0; }
void OverloadTest(char _c, int &_numbits) { int x = 0; }
void OverloadTest(long long _l, int &_numbits) { int x = 0; }
void OverloadTest(long long *_l) { int x = 0; }
void OverloadTest(bool b) { int x = 0; }

我什至试图简化:

void Date::ReflectToBitBuffer(void)
{
unsigned int offsetBits = 0;
unsigned int testVar2 = 12;
unsigned int testVar1 = 1977;

BitPacker::PackBits(m_packedBuffer, testVar1, offsetBits, testVar2);
}

而且我仍然有歧义。但是,如果我使用 float ,则不会。目前我完全不明白为什么我的测试场景没有产生这个错误。任何人都能够阐明问题是什么,因为在我看来第一个代码示例中的函数原型(prototype)签名足够不同以允许它编译。<​​/p>

最佳答案

看起来“int”可以转换为重载函数的所有 4 种类型,但没有一种与您的调用完全匹配。也许这就是问题所在。尝试使您的调用与函数定义完全匹配或为您的调用制作一个 int 版本?

关于c++ - 不明确的重载函数 - 如何以及为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10038637/

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