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

时间:2012-04-06 03:02:44

标签: c++ overloading ambiguity

我在弄清楚是什么让某些函数调用模棱两可而其他人没问题时遇到了麻烦。我在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);
}

我仍然感到含糊不清。如果我使用浮子,但我没有。目前我完全混淆了为什么我的测试场景不会产生这个错误。是否有人能够解释问题是什么,因为第一个代码示例中的函数原型签名看起来与我不同以允许它进行编译。

1 个答案:

答案 0 :(得分:2)

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