I have a problem on Visual Studio 2017 (15.9.58) with an overloaded function :
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
When USE_FIRST_IMPLEM is defined, I get the following error :
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> ]
Note PixelValueT. It isn't defined anywhere in the concerned code, but it is elsewhere, in a class not used here :
template <typename PixelValueT>
class ImageBuffer
{
public:
using pixel_value_t = PixelValueT;
using container_t = std::vector<PixelValueT>;
...
};
It seems the compiler is confused, making a link between the std::vector<GrayScalePixel> used in the function's signature, and ImageBuffer::container_t (these types are actually the same).
I don't define USE_FIRST_IMPLEM, I have no problem.
As found by @Yksisarvinen and @dewaffled, I forgot a const in the signature of the first function.