Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: binary '<': no operator found which takes a left-hand operand of type 'const _Ty  (Read 224 times)

0 Members and 1 Guest are viewing this topic.

Pops

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Hi, I'm new to VS/SFML/C++, haven't done any serious programming since the early 2000's, now just a hobby when I get time. I'm trying to write a "pixel fight", something like but on a map with land and sea. I'm getting the message in the title despite not having a "<" operator in my code. I've looked for a solution online, this seems to be a common error, but I can't find in my code where I have gone wrong. I'm using:

sfml 2.6.1
vs   2022 17.8.3
c++  Visual C++ 2022

If I copy/paste the error message from VS I get a bit more information:

Severity   Code   Description   Project   File   Line   Suppression State   Details
Error   C2678   binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)
        with
        [
            _Ty=sf::Color
        ]   sfmlWindow   C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\include\type_traits   2441

So it looks like I'm doing something wrong when processing pixel colours. I'm hoping someone can point me to where I am going wrong, or if I am diving in too fast, what I need to know to fix this.

Code is:
//#include <iostream>          // cin, cout
#include <set>               // std::set
#include <SFML/Graphics.hpp> //sfml

using namespace std;         // cin, cout

int main()
{
    int const screenwidth  = 1915;
    int const screenheight = 975;
   
    sf::RenderWindow window(sf::VideoMode(screenwidth, screenheight), "TheWarGames");
    sf::Sprite map;
    sf::Texture territory;
   
    if (!territory.loadFromFile("C:\\jgstuff\\Systems\\TheWarGames\\Catalhoyukmap.png"))
        return -1;
    map.setTexture(territory);
    //#########################################################################################
    int random_pixel(int);
    int ct0, ct1, ct2, w, h, wm1, hm1, wp1, hp1, land_ct, enemy_ct, pixel_out;
    const sf::Color green = sf::Color(0x00ff00ff); //  green opaque
    const sf::Color blue = sf::Color(0x0000ffff); //  blue opaque
    const sf::Color red = sf::Color(0xff0000ff); //  red opaque
    const sf::Color black = sf::Color(0x000000ff); //  black opaque
    sf::Color tempcol;
    sf::Color center_pixel_col;
    sf::Color surr_pixels[9];
    const std::set<sf::Color>landorsea={ green, blue };
    const std::set<sf::Color>noncombatant={ green, blue, black};
    const int wto = screenwidth - 1;
    const int hto = screenheight - 1;
    //#########################################################################################

    while (window.isOpen())
    {
       sf::Event event;
       while (window.pollEvent(event))
       {
           if (event.type == sf::Event::Closed)
               window.close();
       }
// Stuff Start
       sf::Image image = territory.copyToImage();
       //int x = rand() % 500;
       //int y = rand() % 500;
       //for (int i = x; i < x+500; i++)
       //{
       //   for (int j = y; j < y+250; j++)
       //   {
       //       image.setPixel(i, j, sf::Color::Black);
       //   }
       //}
       territory.loadFromImage(image);
 //#########################################################################################
// map surrounded by black pixels
// map pointed to by image
//       #### need two images / textures ####
       for (w = 1; w <= wto; w++)
       {
           for (h = 1; h <= hto; h++)
           {
               center_pixel_col = image.getPixel(w, h);
               if (landorsea.find(center_pixel_col) == landorsea.end() &&
                   center_pixel_col.a == 255) // i.e. is fully opaque
               {
                   wm1 = w - 1;
                   hm1 = h - 1;
                   wp1 = w + 1;
                   hp1 = h + 1;
                   ct0 = 0;
                   for (ct1 = wm1; ct1 <= wp1; ct1++)
                   {
                       for (ct2 = hm1; ct2 <= hp1; ct2++)
                       {
                           surr_pixels[ct0] = image.getPixel(ct1, ct2);
                           ct0 += 1;
                       }
                   }
                   land_ct = enemy_ct = 0;
                   for (ct0 = 0; ct0 <= 8; ct0++) // implies 9 pixels pixel in hand and neighbours
                   {
                       if (ct0 != 4)
                       {
                           tempcol = surr_pixels[ct0];
                           if (tempcol == green) // ####################################### HERE ######################
                           {
                               land_ct += 1;
                           }
                           else if (noncombatant.find(tempcol) == noncombatant.end())
                           {
                               enemy_ct += 1;
                           }
                       }
                   }
                   if (land_ct > 0)
                   {
                       pixel_out = random_pixel(land_ct);
                       if (pixel_out != 4)
                       {
                           ct0 = ct1 = 0;
                           while (ct0 <= pixel_out)
                           {
                               if (surr_pixels[ct1] == green &&
                                   ct1 != 4)
                               {
                                   ct0++; // land count
                               }
                               ct1++; // array count
                           }
                           ct1 = random_pixel(2);
                           if (ct1 == 1)
                           {
                               center_pixel_col.a = 254;
                               image.setPixel(wm1 + (ct0 / 3), hm1 + (ct0 % 3), center_pixel_col);
                           }
                           else
                           {
                               tempcol = image.getPixel(wm1 + (ct0 / 3), hm1 + (ct0 % 3));
                               tempcol.a = 254;
                               image.setPixel(w, h, tempcol);
                           }
                       }
                   }
                   else if (enemy_ct > 0)             //enemies not set up yet
                   {
                       pixel_out = random_pixel(enemy_ct);
                       if (pixel_out != 4)
                       {
                           ct0 = ct1 = 0;
                           while (ct0 <= pixel_out)
                           {
                              if (noncombatant.find(center_pixel_col) == noncombatant.end() &&
                                  ct1 != 4)
                              {
                                   ct0++; // enemy count
                              }
                               ct1++; // array count
                           }
                           ct1 = random_pixel(2);
                           if (ct1 == 1)
                           {
                               center_pixel_col.a = 254;
                               image.setPixel(wm1 + (ct0 / 3), hm1 + (ct0 % 3), center_pixel_col);
                           }
                           else
                           {
                               tempcol = image.getPixel(wm1 + (ct0 / 3), hm1 + (ct0 % 3));
                               tempcol.a = 254;
                               image.setPixel(w, h, tempcol);
                           }
                       }
                   }
               }
           }
       }
       //reset changed flags

 //#########################################################################################
       // Stuff End

       window.clear();
       window.draw(map);
       window.display();
    }

    return 0;
}

int random_pixel(int pixel_ct)
{
   srand((unsigned)time(NULL));
   return((rand() % pixel_ct));
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
It's common to see operator < being used when sorting is involved as that's how most sorting decides on the order: "is it less than? yes, then it goes first."

You are using std::set and this stores things in order. Your set has sf::Colors. The question is: how do you sort colours? The set will be asking the same question: "is this colour less than that colour? if yes, it goes first". But wait! How can an sf::Color be "less" than another sf::Color?

You can still do it in a set but you will need extra work. For example, you could put each colour in a struct and define its < operator, then use that struct in the set. You could also add to the struct a value that would allow you to give values to each colour, allowing them to be in a specific order.

However, I think it's unlikely you actually want to order the colours so maybe the unordered version of the set is what you actually want:
https://en.cppreference.com/w/cpp/container/unordered_set

With that said, if you're just storing them, would just a standard vector not be sufficient?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Pops

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Thanks, it was the two std::set definitions, should have spotted that, I assumed it was something way more complicated, have just taken them out and will do without them, for now.

 

anything