J
Jules
I have the following code which isn't working as I expect it to:
---
#include <iostream>
using namespace std;
class mystring
{
public:
mystring (const char * src) { /* ... */ }
};
template<class K, class V>
class Map
{
public:
// ...
virtual V& operator[] (K& value) = 0;
};
template<class V>
class Map<mystring, V>
{
public:
// ...
virtual V& operator[] (mystring& value) = 0;
// plus additional operator:
V& operator[] (const char * v) {
mystring str(v);
return (*this)[str];
}
};
template<class K, class V>
class Hashtable : public Map<K, V>
{
V test;
public:
virtual V& operator[] (K& value) { return test; }
};
int main (void)
{
Hashtable<mystring, int> test;
cout << test["hello"] << endl;
}
---
When I attempt to compile this, I get the following error message from
g++ (3.3):
tps.cpp: In function `int main()':
tps.cpp:43: error: no match for 'operator[]' in 'test["hello"]'
tps.cpp:37: error: candidates are: V& Hashtable<K, V>:perator[](K&)
[with K = mystring, V = int]
This isn't what I expect to happen because Hashtable<mystring,int> is
derived from Map<mystring,int> which should (surely?) be expanded based
on the partially specialized template which includes an
'operator[](const char *)' that should match this call. Everything
works fine if I take out the inheritance and specialize Hashtable
directly, but I want to be generic in implementation and have an
abstract base class with all the methods declared...
Anyone know why this doesn't work?
(please only reply to the group; I can't currently read e-mail sent to
the address quoted above)
---
#include <iostream>
using namespace std;
class mystring
{
public:
mystring (const char * src) { /* ... */ }
};
template<class K, class V>
class Map
{
public:
// ...
virtual V& operator[] (K& value) = 0;
};
template<class V>
class Map<mystring, V>
{
public:
// ...
virtual V& operator[] (mystring& value) = 0;
// plus additional operator:
V& operator[] (const char * v) {
mystring str(v);
return (*this)[str];
}
};
template<class K, class V>
class Hashtable : public Map<K, V>
{
V test;
public:
virtual V& operator[] (K& value) { return test; }
};
int main (void)
{
Hashtable<mystring, int> test;
cout << test["hello"] << endl;
}
---
When I attempt to compile this, I get the following error message from
g++ (3.3):
tps.cpp: In function `int main()':
tps.cpp:43: error: no match for 'operator[]' in 'test["hello"]'
tps.cpp:37: error: candidates are: V& Hashtable<K, V>:perator[](K&)
[with K = mystring, V = int]
This isn't what I expect to happen because Hashtable<mystring,int> is
derived from Map<mystring,int> which should (surely?) be expanded based
on the partially specialized template which includes an
'operator[](const char *)' that should match this call. Everything
works fine if I take out the inheritance and specialize Hashtable
directly, but I want to be generic in implementation and have an
abstract base class with all the methods declared...
Anyone know why this doesn't work?
(please only reply to the group; I can't currently read e-mail sent to
the address quoted above)