0

VS 2010 is consistently giving me this error:

C2678: binary '==' : no operator found which takes a left-hand operand of type 'const AnyColor' (or there is no acceptable conversion)

For this expression:

bool AnyColor::operator ==(const AnyColor &)

While trying to match the argument list (const AnyColor, const AnyColor)

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Logicrat
  • 4,438
  • 16
  • 22
  • 3
    `operator==` you wrote cannot be called on `const AnyColor` object. You probably meant `bool operator==(const AnyColor& op) const { ... }` (note the second `const` keyword) – Igor Tandetnik Oct 30 '21 at 21:54
  • 2
    Your `operator==` needs to be a `const` member function (`bool operator==(const AnyColor &b) const;`. Same for `operator!=`. – 1201ProgramAlarm Oct 30 '21 at 21:54
  • Thanks for the suggestion. I added `const` after the parameter list in `operator==` and now get `c:\program files\microsoft visual studio 10.0\vc\include\xfunctional(768): error C2440: 'type cast' : cannot convert from 'const AnyColor' to 'size_t'` – Logicrat Oct 30 '21 at 21:58
  • @Logicrat on which statement? Also, `std::unordered_map` should be `std::unordered_map`. – Remy Lebeau Oct 30 '21 at 22:08

0 Answers0