473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template partial specialisation/inheritance problem

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)

Aug 18 '05 #1
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
Aug 18 '05 #2

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.

Aug 18 '05 #3
> 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...)?

Aug 18 '05 #4
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
Aug 18 '05 #5

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.

Aug 18 '05 #6
>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. :)

Aug 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
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...
7
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
1
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...
6
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...
1
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>
8
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;
1
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,...
8
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:
9
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.