I am working on an embedded project using an mbed. The chip's manufacturer specifies a Cyclical Redundancy Test using this lookup generator, but its written in C. Lookup Generator Code

    ///////////////////////configures CRC check lookup table////////////////////////
short pec15Table[256];
short CRC15_POLY = 0x4599;   //CRC code

void configCRC(void) 
{
    for (int i = 0; i < 256; i++)
    {
        remainder = i << 7;
        for (int bit = 8; bit > 0; --bit)
        {
            if (remainder & 0x4000)
            {
                remainder = ((remainder << 1));
                remainder = (remainder ^ CRC15_POLY)
            }
            else
            {
                remainder = ((remainder << 1));
            }
        }
    }
    pec15Table[i] = remainder&0xFFFF;
};

I am not really good with C++ yet, so I just copied and pasted it and checked for clear syntax errors. For example I switched the int16 declarations with short and unsigned short. But, when I compile it gives me the following error. Which doesn't make sense to me. I am sure im doing something wrong.

Error: Cannot determine which instance of overloaded function "remainder"  is intended in "config.cpp", Line: 20, Col: 10

2 Answers

0
Babajan On

Just rename variable remainder to fremainder( or some other name as you wish) and see the magic in compilation.

These kind of issues come into picture because of not following any standard convention while naming variable.

Check this link to see why renaming of variable is required

1
Lundin On

Obviously you have a namespace collision with std::remainder. This is one of many reasons to avoid global variables. C and C++ should otherwise be identical here.

Notably though, this code is very naively written. Not only must the function be rewritten to properly take parameters, but the type use is all over the place.

You should never do bit-wise arithmetic on signed types, because that opens up for a lot of poorly-defined behavior bugs. All "sloppy typing" types like short and int must be replaced with types from stdint.h. You should only use unsigned types. You need to be aware of implicit integer promotion.