Can you overload operators

Posted on 2005-04-12
Last Modified: 2010-04-01

I made a struct that I defined some < operators for so I could use it for the key of an std::map<>. My first operator works fine, my second one looks like it should too but I always get a compilation error trying to use it. Can you not overload operators maybe?

struct MINE {
    int n;
    char sz[20];

    // This bool operator works fine.
    bool operator < (const MINE &m) const {
        if (strcmp(is.szSeriesUID, szSeriesUID) == 1)
            return true;
            return false;

    // This one always throws a compilation error when I try to use it.
    bool operator < (const char *psz) {
        if (strcmp(psz, sz) == 1)
            return true;
            return false;

And here is how I use it:

map<MINE, int> my_map;
MINE mine;

my_map[mine]; // works fine;

my_map["hi there"]; // no operator found which takes right hand op of type const char[8]

Any ideas?

Question by:AlreadyGoogled
    LVL 8

    Expert Comment

    map<char*, int> or
    map<string, int> should work for you.


    Author Comment

    I could, but is there a way to make this work? I'm interested why this is illegal?
    LVL 1

    Expert Comment

    I was under the impression that you could pass STL containers a comparator object. A comparator object is an object that defines operator () and accepts 2 parameters and returns -1 if a > b, 0 a==b and 1 if a < b.
    LVL 8

    Expert Comment

    The [] operator of the map class is looking for a key type.  It cannot convert your input const char[8] to a MINE object.

    LVL 1

    Assisted Solution

    If you have a MINE constructor accept a char*, the compiler may perform the implicit casting for you from a char* to a MINE.
    LVL 3

    Expert Comment

    Have you tried:
    const char* tmp( "Hi there");

    Just to check what compiler message you get?
    LVL 5

    Accepted Solution

    The type for your key of  map is MINE, and index operator of map (which is []) is overloaded.
    When you use index operator to access map elements, you can only use MINE as a index type.
    So have to decide which type you really need : string or MINE.
    If your class MINE has a constructor MINE(const string&), and you can access elements as
    MINE mine("hi there");
    good luck.

    Author Comment

    I see now, thanks a lot for your answers everyone. Eventually I decided to just make the key a <string> instead of my struct, it will just be more convenient in the end, though I did get it to work with the struct by your suggestions, thanks!

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    cb: unreferenced local variable 11 59
    Getting IP address 8 56
    C++ to C# code conversion issue 4 73
    convert char array to number in c 5 65
    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now