Okay, I'm trying to make a quick little class to work as a sort of hash table. If I can get it to work then I should be able to do this:
StringHash* hash = new StringHash;
hash["test"] = "This is a test";
printf(hash["test"]);
And it should print out "This is a test".
It looks like I have 2 problems at this point. Firstly I did this:
const char* operator[](const char* key) {
for(int i = 0; i < hashSize; ++i) {
if(strcmp(hkeys[i], key) == 0) {return values[i];}
}
return NULL;
}
But when I try to look up a value the compiler complains that
error: invalid types `StringHash*[const char[5]]' for array subscript
Secondly operator[]= does not appear to be the correct syntax here. The only other thing I could find was &operator[] but I don't think that will work since I have to code the lookup procedure??? (Isn't that syntax just used to return an array item reference?)
Is what I'm trying to do here even possible? Any advice appreciated. :)
Seems to be some confusion about what I'm trying to do. I'll post my code:
http://pastebin.com/5Na1Xvaz
Finished product after all the help:
http://pastebin.com/gx4gnYy8
The name of an overloaded operator is operator x, where x is the operator as it appears in the following table. For example, to overload the addition operator, you define a function called operator+. Similarly, to overload the addition/assignment operator, +=, define a function called operator+=.
Operator Overloading in C++ This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.
Polymorphism: Polymorphism (or operator overloading) is a manner in which OO systems allow the same operator name or symbol to be used for multiple operations. That is, it allows the operator symbol or name to be bound to more than one implementation of the operator. A simple example of this is the “+” sign.
Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.
The error is because hash
is a pointer. Change to:
StringHash hash;
The other answers relate to your first question. As for your second...
If you return a reference, then you're returning an lvalue. You can always assign to an lvalue.
Yes, it (pretty much) really is that simple. I recommend reading carefully for whether or not you need const
in various places, though.
What I remember reading is that you should provide a const
and a non-const
overload for operator[]
, something like so:
MyType const &operator[](int index) const; // This is the array access version (no assignment allowed), which should work on const objects
MyType &operator[](int index); // This is the array access or assignment version, which is necessarily non-const.
See this link for more information.
hash
isn't a StringHash
object. Its a pointer to one.
You can do this:
(*hash)["test"] = "This is a test";
Or you can ask yourself why you need a pointer to it in the first place,
StringHash hash;
hash["test" = "This is a test";
... or even if you do, why you wouldn't use a smart pointer like auto_ptr
.
#include <memory>
std::auto_ptr<StringHash> hash( new StringHash );
(*hash)["test"] = "This is a test";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With