views:

75

answers:

3

I have been trying to make a StringTable class that holds a simple unordered_map<string, string>, and has the array index operator '[]' overloaded to work for accessing the map; however, the compiler will tell me that I have yet to define the overloaded operator when I try to use it. My code is as follows:

CStringTable.h

#include <string>
#include <fstream>
#include <tr1/unordered_map>

class CStringTable {
public:
    bool Load(const char* filename);

    inline const char* operator [](const char* key);

    const char* Get(const char* key);

private:
    std::tr1::unordered_map<std::string, std::string>* m_StringMap;
};

CStringTable.cpp

#include "CStringTable.h"

inline const char* CStringTable::operator [](const char* key) {
    std::string sKey = key;

    return (*m_StringMap)[sKey].c_str();
}

I try to access the map as follows:

(*m_cStringTable)[msgKey]

where m_cStringTable is a pointer to an instance of the CStringTable class and msgKey is a const char*.

Can anybody enlighten me as to why this won't work?

A: 

It seems that the type unordered_map has not the operator [] defined. Are you sure you can access the element of the map with that operator?

rano
I do so in my Get() function that should ahve the same functionality, and that function seems to work. The code is literally copy and pasted.const char* CStringTable::Get(const char* key) { std::string sKey = key; return (*m_StringMap)[sKey].c_str();}
Chris Covert
No, it supports: http://msdn.microsoft.com/en-us/library/bb982266.aspx
Dewfy
Aw so this is where this library come from : )Are you sure you really need it?
rano
I actually have it through MinGW gcc anyway. :)
Chris Covert
+4  A: 

Regarding the inline keyword, the compiler needs to be able to see the body of that method as well in the .h file. So either move the implementation of the operator from the .cpp file to the .h file, or include the body of the operator in the class declaration.

Frederik Slijkerman
Ahh yes, this appears to have fixed the problem. Either removing the inline keyword or moving the declaration to the .h file. Thank you.
Chris Covert
A: 

Here it is, per spec of unordered_map:

If inserting an element causes unordered_map::load_factor() to exceed the maximum load factor, the container increases the number of buckets and rebuilds the hash table as needed.

I'm not sure about engine of reallocation, but seems string would be copied by value. This mean that when you return const char * - in fact it can be erased by container reallocation. So pointer is unsafe and non-const.

On other hand (for example usual std::map) grants that tree is stable relative insert/remove operators and memory pointers of std::string are stable

Dewfy
How would this cause a compiler error about overloading `[]`?
Michael Mrozek
@Michael Mrozek I've asked to provide compiler error. Since I didn't get it I begin "sniffing" what else wrong with this code. My point with reallocation looks reasonable, isn't?
Dewfy
@Dewfy I've no idea if this is right, but if it doesn't answer his question you shouldn't post it as an answer; it's a comment
Michael Mrozek