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>::operator[](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) 6 1988
Jules wrote: 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;
You might consider making it
virtual V& operator[] (K const& value) = 0;
};
template<class V> class Map<mystring, V> { public: // ... virtual V& operator[] (mystring& value) = 0;
Here as well... // 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): [...]
V
Jules wrote: 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>::operator[](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)
the function in the base is hidden. see the FAQ.
> the function in the base is hidden. see the FAQ.
Err, OK. Don't know how I missed that before. :(
I think I understand now... but I'm having trouble trying to figure out
a workaround. How do I change it so that this works without explicitly
specializing Hashtable (and potentially BTreeMap, and
ReferenceToJavaMap, and ReferenceToPythonDictionary, etc...)?
Jules wrote: the function in the base is hidden. see the FAQ.
Err, OK. Don't know how I missed that before. :(
I think I understand now... but I'm having trouble trying to figure out a workaround. How do I change it so that this works without explicitly specializing Hashtable (and potentially BTreeMap, and ReferenceToJavaMap, and ReferenceToPythonDictionary, etc...)?
Add
using Map<K,V>::operator[];
to the public section of Hashtable template.
V
Jules wrote: 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:
using Map<K, V>::operator[];
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>::operator[](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)
Jules wrote: Alipha wrote: the function in the base is hidden. see the FAQ.
Err, OK. Don't know how I missed that before. :(
I think I understand now... but I'm having trouble trying to figure out a workaround. How do I change it so that this works without explicitly specializing Hashtable (and potentially BTreeMap, and ReferenceToJavaMap, and ReferenceToPythonDictionary, etc...)?
see above addition to your code.
>Add using Map<K,V>::operator[];
to the public section of Hashtable template.
Thank you! Didn't realise I could refer to it like that without
specifying the type of parameter. :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Paul MG |
last post by:
Hi
Template partial specialization always seems like a fairly
straightforward concept - until I try to do it :).
I am trying to implement the input sequence type (from Stroustrup
section...
|
by: Lionel B |
last post by:
Greetings.
The following code compiles ok and does what I'd expect it to do:
---------- START CODE ----------
// test.cpp
|
by: Alfonso Morra |
last post by:
if I have a class template declared as ff:
(BTW is this a partial specialization? - I think it is)
template <typename T1, myenum_1 e1=OK, my_enum_2=NONE>
class A {
public:
A();
virtual...
|
by: rincewind |
last post by:
Hi,
can anybody summarise all options for partial template specialization,
for all kind of parameters (type, nontype, template)?
I *think* I understand options for partial specialization on...
|
by: Nathan Addy |
last post by:
I am trying to partially specialize a template in the following way:
I have two template classes defined:
template <typename T>
class Foo
{
public:
Foo();
};
template <typename T>
|
by: Paul Roberts |
last post by:
Hi,
I'm hoping somebody here can help me with a simple problem of template
syntax.
Here's an example:
template<typename T, int iclass A
{
static int a;
|
by: Martin |
last post by:
I'm trying to make a partial specialization of a class of mine. Can
someone please tell me what's wrong with the following code? GCC gives
me the error "invalid use of undefined type "class X<int,...
|
by: Rahul |
last post by:
Hi,
Is there a way to partially specialize only a member function of a
template class (not the whole class).
e.g.
template <typename A, typename B>
class Base
{
public:
|
by: stephen.diverdi |
last post by:
Can anyone lend a hand on getting this particular template
specialization working? I've been trying to compile with g++ 4.1 and
VS 2005.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |