samedi 30 octobre 2021

No operator found even though I think I wrote one

VS 2010 is consistently giving me error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const AnyColor' (or there is no acceptable conversion) for expression 'bool AnyColor::operator ==(const AnyColor &)' while trying to match the argument list '(const AnyColor, const AnyColor)'
br> Here is the entire program:

#include "stdafx.h"
#include <string.h>
#include <stdint.h>
#include <unordered_map>
#include <cstdlib>
#include <iostream>

//  ________________________________________________________________________________

typedef uint16_t    cSpace;

//  Identifiers for different color spaces
enum    {
    kCsRGB = 0,
    kCsCMYK,
    kCsGray
};

//  The color class can represent colors from a variety of spaces.
//  Up to a 4 dimensional color can be represented.
class   AnyColor    {
public:
    AnyColor(void)  {
        space = kCsRGB; // Might change; this is an initial guess to avoid meaningless values
        memset(axis, 0, sizeof(axis));
    };

    AnyColor(short type, double a, double b, double c, double d)
    {
        space   =   type;
        axis[0] =   a;
        axis[1] =   b;
        axis[2] =   c;
        axis[3] =   d;
    }

    void Set(short type, double a, double b, double c, double d)
    {
        space   =   type;
        axis[0] =   a;
        axis[1] =   b;
        axis[2] =   c;
        axis[3] =   d;
    }

    inline  bool    operator==(const AnyColor& op) // Equal only if all its members are equal
    {
        if  (space != op.space)
        {
            return  false;
        }

        for(int i = 0; 4 > i; i++)
        {
            if  (axis[i] != op.axis[i])
            {
                return  false;
            }
        }
        return  true;
    }

    inline bool operator!=(AnyColor &op)
    {
        return (*this == op) ? false : true;
    }

    cSpace  space;
    double  axis[4];
};

static std::unordered_map<const AnyColor,int> colormap;
static int seq = -1;
const int numColors = 25;

static double quarters()
{
    return 0.25 * static_cast<double>(rand() % 5);
}

int _tmain(int argc, _TCHAR* argv[])
{
    AnyColor c;

    for(int i = 0; numColors > i; ++i)
    {
        c.axis[0] = quarters();
        c.axis[1] = quarters();
        c.axis[2] = quarters();
        if (colormap.count(c) == 0) // THIS IS WHERE THE PROBLEM SHOWS UP
        {
            colormap[c] = ++seq;
        }
    }
    return 0;
}

I've tried all combinations of passing the parameter by value vs by reference and const vs not const. I get the same error every time. I bet I'm overlooking something simple.

This program does nothing; I was just starting to write it when I encountered this error. I hope to end up with code that assigns sequence numbers to unique colors.

I have to be able to compile with VS2010 so my C++ dialect is limited to C++11.

In this little test segment, I generate random colors just to test my use of unordered_map. What do I need to do to get this to compile?

Aucun commentaire:

Enregistrer un commentaire