Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Can you overload operators

Posted on 2005-04-12
8
Medium Priority
?
240 Views
Last Modified: 2010-04-01
Hi,

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];

    MINE(){}
   
    // This bool operator works fine.
    bool operator < (const MINE &m) const {
        if (strcmp(is.szSeriesUID, szSeriesUID) == 1)
            return true;
        else
            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;
        else
            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?

Thanks!
0
Comment
Question by:AlreadyGoogled
8 Comments
 
LVL 8

Expert Comment

by:novitiate
ID: 13769053
map<char*, int> or
map<string, int> should work for you.

_novi_
0
 

Author Comment

by:AlreadyGoogled
ID: 13769068
I could, but is there a way to make this work? I'm interested why this is illegal?
0
 
LVL 1

Expert Comment

by:VolatileVoid
ID: 13769211
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

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

corey
0
 
LVL 1

Assisted Solution

by:VolatileVoid
VolatileVoid earned 360 total points
ID: 13769232
If you have a MINE constructor accept a char*, the compiler may perform the implicit casting for you from a char* to a MINE.
0
 
LVL 3

Expert Comment

by:balder
ID: 13769657
Have you tried:
const char* tmp( "Hi there");
my_map[tmp];

Just to check what compiler message you get?
0
 
LVL 5

Accepted Solution

by:
rendaduiyan earned 360 total points
ID: 13771076
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
following:
MINE mine("hi there");
my_map[mine];
good luck.
0
 

Author Comment

by:AlreadyGoogled
ID: 13772294
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!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

580 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