2

我很难弄清楚是什么使某些函数调用模棱两可,而其他函数调用则很好。我的 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);
}

我仍然有歧义。如果我使用浮点数,但我不会。目前我完全困惑为什么我的测试场景不会产生这个错误。任何人都能够阐明问题是什么,因为第一个代码示例中的函数原型签名在我看来是不同的足以允许它编译。

4

1 回答 1

2

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

于 2012-04-06T03:11:21.467 回答