我有这个代码使用lodepng库来加载PNG文件。图书馆没问题,成功地用于其他项目,没有问题。
const std::string tmpString = mapFileName.GetConstString();
std::vector<unsigned char> xx;
unsigned int error = lodepng::decode(xx, (unsigned int)this->mapWidth, (unsigned int)this->mapHeight, tmpString, LCT_GREY, (unsigned int)8);
我想编译它,但得到奇怪的错误信息。
MapHelper.cpp(72): error C2665: 'lodepng::decode' : none of the 5 overloads could convert all the argument types
c:\ImageUtils\lodepng.h(200): could be 'unsigned int lodepng::decode(std::vector<_Ty> &,unsigned int &,unsigned int &,const unsigned char *,size_t,LodePNGColorType,unsigned int)'
with
[
_Ty=uint8
]
c:\ImageUtils\lodepng.h(203): or 'unsigned int lodepng::decode(std::vector<_Ty> &,unsigned int &,unsigned int &,const std::vector<_Ty> &,LodePNGColorType,unsigned int)'
with
[
_Ty=uint8
]
c:\ImageUtils\lodepng.h(211): or 'unsigned int lodepng::decode(std::vector<_Ty> &,unsigned int &,unsigned int &,const std::string &,LodePNGColorType,unsigned int)'
with
[
_Ty=uint8
]
c:\ImageUtils\lodepng.h(759): or 'unsigned int lodepng::decode(std::vector<_Ty> &,unsigned int &,unsigned int &,lodepng::State &,const unsigned char *,size_t)'
with
[
_Ty=uint8
]
while trying to match the argument list '(std::vector<_Ty>, unsigned int, unsigned int, const std::string, LodePNGColorType, unsigned int)'
with
[
_Ty=uint8
]
我看不出什么是错的,输入参数的类型与库中的相同,并且类型中不会发生冲突。
修改
函数,我lodepng::decode
不是const
答案 0 :(得分:4)
好吧,我原本期望得到一个更准确的错误信息,但考虑到此签名存在重载
'(std::vector<_Ty> &,unsigned int &,unsigned int &,const std::string &, LodePNGColorType, unsigned int)'
与您自己的调用相比
'(std::vector<_Ty>, unsigned int, unsigned int, const std::string, LodePNGColorType, unsigned int)'
那么问题必然在于前者在提供unsigned int&
时需要unsigned int
。虽然这通常很好,但在这种情况下并不是因为你在函数调用中转换为unsigned int
,因此创建一个R值。所以试试这个:
unsigned int wd = (unsigned int)this->mapWidth,
ht = (unsigned int)this->mapHeight;
unsigned int error = lodepng::decode(xx, wd, ht, tmpString, LCT_GREY, (unsigned int)8);
通过引用传递通常用于将输出返回给调用代码。如果wd
,ht
仅用作输出,那么实际上您不需要初始化它们
unsigned int wd, ht;
足够好了。无论哪种方式wd
,ht
在函数返回后都会有新值,您可能需要完成
this->mapWidth = wd;
this->mapHeight = ht;