Ryan Van Dyke
Ryan Van Dyke

Reputation: 35

Ambiguous Overloaded Functions - How and Why?

I'm having serious trouble figuring out what is making certain function calls ambiguous and others are fine. I have the following overloaded function calls within my BitPacker object:

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);

I am trying to make the following call from within another object that is including "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", and "m_day" are int member variables. However I am getting the following errors when attempting to compile:

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, )'

So I wrote a test solution with the following main.cpp to test out overloads and what I have below compiles fine:

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; }

I even tried to simplify:

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

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

And I still get the ambiguity. If I use a float however I don't. At the moment I am utterly confused why my test scenario does not produce this error. Would anyone be able to shed some light on what the problem is because the function prototype signatures in the first code example look to me to be different enough to allow it to compile.

Upvotes: 2

Views: 2833

Answers (1)

xxbidiao
xxbidiao

Reputation: 874

It looks like that "int" can be casted into all 4 types of your overloaded function and none of them exactly matches your call. Maybe that's the problem. Try making your call exactly match the function definiton or making an int version for your call?

Upvotes: 2

Related Questions