已解决
我在 Visual Studio 2017 (15.9.58) 上遇到重载函数问题:
using GrayScalePixel = std::uint8_t;
struct PgmImage
{
std::uint16_t Width;
std::uint16_t Height;
std::vector<GrayScalePixel> PixelValues;
};
void printPixelValues(std::uint16_t width, std::uint16_t height, std::vector<GrayScalePixel>& pixelValues)
{
auto savedFillingChar = std::cout.fill('0');
for (int i = 0; i < width; ++i)
{
for (int j = 0; j < height; ++j)
{
std::cout << std::setw(3) << (int)pixelValues[i * width + j] << " ";
}
std::cout << std::endl;
}
std::cout.fill(savedFillingChar);
}
#define USE_FIRST_IMPLEM
#ifdef USE_FIRST_IMPLEM
void printPixelValues(const PgmImage& pgmImage)
{
printPixelValues(pgmImage.Width, pgmImage.Height, pgmImage.PixelValues);
}
#else
void printPixelValues(const PgmImage& pgmImage)
{
std::uint16_t width = pgmImage.Width;
std::uint16_t height = pgmImage.Height;
std::vector<GrayScalePixel> pixelValues = pgmImage.PixelValues;
printPixelValues(width, height, pixelValues);
}
#endif
定义 USE_FIRST_IMPLEM 时,出现以下错误:
error C2665: 'printPixelValues': none of the 2 overloads could convert all the argument types
note: could be 'void printPixelValues(uint16_t,uint16_t,std::vector<PixelValueT,std::allocator<_Ty>> &)'
2> with
2> [
2> PixelValueT=GrayScalePixel,
2> _Ty=GrayScalePixel
2> ]
2> note: while trying to match the argument list '(const uint16_t, const uint16_t, const std::vector<PixelValueT,std::allocator<_Ty>>)'
2> with
2> [
2> PixelValueT=GrayScalePixel,
2> _Ty=GrayScalePixel
2> ]
注意PixelValueT。它没有在相关代码中的任何地方定义,但它在其他地方,在此处未使用的类中:
template <typename PixelValueT>
class ImageBuffer
{
public:
using pixel_value_t = PixelValueT;
using container_t = std::vector<PixelValueT>;
...
};
编译器似乎很困惑,在函数签名中使用的
std::vector<GrayScalePixel>
和 ImageBuffer::container_t
之间建立了链接(这些类型实际上是相同的)。
我没有定义
USE_FIRST_IMPLEM
,我没有问题。
@Yksisarvinen 和 @dewaffled 发现,我忘记了第一个函数签名中的 const。