错误C2666:'std :: fpos< _Mbstatet> :: operator +':2个重载具有类似的转换

时间:2014-04-15 05:41:37

标签: c++ visual-c++

我在下面的代码片段中遇到了上述错误,我修复了此错误。但是我不确定修复,如果修复不好,请你告诉我。谢谢你的时间!

代码段:

// get length
//long length = strlen( statement)+5; 
std::streamoff length = strlen(statement) + 5; // Fix for the error

// ckeck length and stream size against max buffer size
if( (m_sqlStream.tellp()+length)>=BATCH_WRITER_BUFFER_SIZE) //c2666

输出:

error C2666: 'std::fpos<_Mbstatet>::operator +' : 2 overloads have similar conversions
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\iosfwd(88): could be 'std::fpos<_Mbstatet> std::fpos<_Mbstatet>::operator +(std::streamoff) const'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\comutil.h(174): or       '_bstr_t operator +(const wchar_t *,const _bstr_t &)'
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\comutil.h(173): or       '_bstr_t operator +(const char *,const _bstr_t &)'
while trying to match the argument list '(std::fpos<_Mbstatet>, long)'

1 个答案:

答案 0 :(得分:0)

我认为是对的。 std::streamsizestd::streamoff必须是整数类型的typedef,并且可以将它们转换为整数类型。 编译器抱怨是因为它无法确定operator +的哪个重载是合适的,因为参数可以隐式转换多个方式。例如:

void f(int, double);
void f(double, int);

int main() {
    f(3, 4);   // C2666! 3 or 4 can be converted to a int or a double, compiler can't resolve this ambiguity.
} 

您可以通过明确地转换一个或多个实际参数来澄清它:

if ((long)m_sqlStream.tellp() + length) >= BATCH_WRITER_BUFFER_SIZE)

或做你刚才所做的事 请注意,编译器可能会在从std::streamoff转换为long时警告您可能丢失数据,因为std::streamoff是由C ++标准库实现定义的有符号整数类型,并且足够大以满足最大可能的文件大小。