我的编程历史记录是C和CPython。请在这里忍受我。使用带有矢量的静态字符串
为了帮助我学习C++,我将其中一个旧的C程序转换为使用C++ OOP,但它并不按照我希望的方式工作。我不在乎速度。我只关心学习。
这里是我的旧的C代码,我想投入校验类:
//This is the standard CRC32 implementation
//"rollingChecksum" is used so the caller can maintain the current
//checksum between function calls
unsigned int CalculateChecksum(unsigned char* eachBlock, int* sbox, long lengthOfBlock, unsigned int rollingChecksum)
{
int IndexLookup;
int blockPos;
for(blockPos = 0; blockPos < lengthOfBlock; blockPos++)
{
IndexLookup = (rollingChecksum >> 0x18)^eachBlock[blockPos];
rollingChecksum = (rollingChecksum << 0x08)^sbox[IndexLookup];
}
return rollingChecksum;
}
因此,这里是我如何把它翻译成更多的C++“安永代码:
void Checksum::UpdateStream(std::vector<unsigned char> binaryData)
{
unsigned int indexLookup;
unsigned int blockPos;
for(blockPos = 0; blockPos < binaryData.size(); blockPos++)
{
indexLookup = (this->checksum >> 0x18)^binaryData[blockPos];
this->checksum = (this->checksum << 0x08)^this->sbox[indexLookup];
}
}
但后来当我尝试使用方法:
int main(int argc, char* argv[])
{
Checksum Test;
Test.UpdateStream("foo bar foobar");
std::cout << Test.getChecksum() << std::endl;
}
我得到这个错误:
1>main.cpp(7) : error C2664: 'Checksum::UpdateStream' : cannot convert parameter 1 from 'const char [15]' to 'std::vector<_Ty>'
1> with
1> [
1> _Ty=unsigned char
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
因为how this question turned out on StackOverflow,我决定使用上面的矢量容器而不是字符串类,因为我想在这里使用二进制数据。
DESIRED RESULT:如何将字符串和二进制数据传递给此方法来计算其校验和?我是否需要重载它或者在主类型中声明字符串?我完全失去了。
...和也许覆盖校验:: UpdateStream,它需要一个字符数组,将其转换为一个std ::矢量和与调用校验:: UpdateStream(标准::矢量<无符号字符> binaryData)。 – thbusch 2011-01-29 17:04:25
我刚刚提出这个建议。 +1。 – 2011-01-29 17:04:31