473,788 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ambiguous superclass functions when specializing STL class

Hi,
I'm trying to extend an STL class with additional iterator
functionality. In the simplified example below I added an extra
iterator class with a dereferencing operator. This operator
internally relies on the at function of the superclass.

#include <vector>

class LongVector: vector< long >
{
class extraiterator
{
int pos;

long& operator*() { return vector< long >::at( pos ); }

/* other functions removed */
};
};

When I try to compile (with Visual C++ 2005 Express) I get following
ambiguous call error on the at() call, apparently between a const and
a non-const variant of the at() method:

error C2668: 'std::vector<_T y>::at' : ambiguous call to overloaded
function
1 with
1 [
1 _Ty=long
1 ]
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\vector(728): could be 'long &std::vector<_T y>::at(unsigne d int)'
1 with
1 [
1 _Ty=long
1 ]
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\vector(721): or 'const long &std::vector<_T y>::at(unsigne d int)
const'
1 with
1 [
1 _Ty=long
1 ]
1 while trying to match the argument list '(int)'
I get similar error for any other function I try to use. Anyone an
idea what I'm doing wrong ? Or how I can force the compiler to take a
specific variant ?

Thanks in advance !

May 4 '07 #1
2 2465
pv********@yaho o.com wrote:
I'm trying to extend an STL class with additional iterator
functionality. In the simplified example below I added an extra
iterator class with a dereferencing operator. This operator
internally relies on the at function of the superclass.
I think you're relying on some kind of special relationship
between an instance of a class and an instance of its nested
class. There isn't any.
>
#include <vector>

class LongVector: vector< long >
{
class extraiterator
{
int pos;

long& operator*() { return vector< long >::at( pos ); }
'extraiterator' has no relation to 'vector<long>'. In order to call
a non-static member of 'vector<long>' class, you need an instance
of 'vector<long>'. The const-ness of that instance will determine
the correct overloaded function to use.
>
/* other functions removed */
};
};

When I try to compile (with Visual C++ 2005 Express) I get following
ambiguous call error on the at() call, apparently between a const and
a non-const variant of the at() method:

error C2668: 'std::vector<_T y>::at' : ambiguous call to overloaded
function
1 with
1 [
1 _Ty=long
1 ]
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\vector(728): could be 'long &std::vector<_T y>::at(unsigne d int)'
1 with
1 [
1 _Ty=long
1 ]
1 C:\Program Files\Microsoft Visual Studio 8\VC\include
\vector(721): or 'const long &std::vector<_T y>::at(unsigne d int)
const'
1 with
1 [
1 _Ty=long
1 ]
1 while trying to match the argument list '(int)'
I get similar error for any other function I try to use. Anyone an
idea what I'm doing wrong ?
Here is a short example:

struct B { void foo(); };

struct A : B {
struct nested {
void bar() { B::foo(); /* error here */ }
};
};

Since 'B::foo' is non-static, you cannot call it like that inside
the 'A::nested::bar '. You have to have an instance of A. You can
either pass it as an argument to 'bar' or have it a member of the
'nested' class:

...
struct nested {
void bar(A &a) { a->B::foo(); }
};

or

...
struct nested {
A a;
void bar() { a->B::foo(); }
};
Or how I can force the compiler to take a
specific variant ?
No need to force anything. Just make sure it knows what object
to call the member for.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '07 #2
pv********@yaho o.com skrev:
Hi,
I'm trying to extend an STL class with additional iterator
functionality. In the simplified example below I added an extra
iterator class with a dereferencing operator. This operator
internally relies on the at function of the superclass.

#include <vector>

class LongVector: vector< long >
{
class extraiterator
{
int pos;

long& operator*() { return vector< long >::at( pos ); }

/* other functions removed */
};
};
The iterator needs to operate on some object.

class LongVector : vector<long{
class extraiterator {
LongVector * d_longvector;
int pos;
long& operator*() { return d_longvector->at(pos); }
extraiterator(L ongVector * lv) : d_longvector(lv ) {}
};
};

--
OU
May 4 '07 #3

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

Similar topics

5
1478
by: Bolin | last post by:
I am frustrated as I am trying to design a toy image library. I want the user to write mul(im, 2) if s/he wants to multiply the image by 2, and mul(im1, im2) if s/he wants to multiply two images together. I would have like to
2
3036
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass' priviledged methods also. Do you know if this is possible ? In the following example, I create an ObjectA (variable a), an ObjectB which inherits ObjectA (variable b) and an ObjectC which inherits ObjectA (variable c1). The 'toString ()' method...
9
3976
by: xuatla | last post by:
compile error: test1.cpp:21: error: ISO C++ says that `T mtd::CDiffOperator::getdp(const mtd::mVector&, long int, mtd::mBCTYPE) const' and `void mtd::CDiffOperator::getdp(mtd::mVector&, const mtd::mVector&, mtd::mBCTYPE) const' are ambiguous even though the worst conversion for the former is better than the worst conversion for the latter
19
4261
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
1
1278
by: Nishith Prabhakar | last post by:
Hi, I have a class which inherits from a template (vector). This class is defined in the header file as below. Output.h class _DLL_RESPONSE_SERVER Output : public vector<SingleOutput> { public:
2
1461
by: esuvs81 | last post by:
Hi guys, is it possible to use partial specialization on individual member functions of a class (rather than partially specializing the whole class)? I have the following code: #include <iostream> template<typename T, typename U> class MyClass { public:
14
4083
by: Marko | last post by:
I have abstract superclass named Element, and several subclasses derived from it: And, Or, Nand, Nor... I have method which has to take 2 subclasses of element as its parameters types. Can't use void myFunction(Element& e1,Element& e2); prototype because I am trying to pass it objects of class And or any other subclass. Need help..
9
2204
by: neildferguson | last post by:
I am using templates with a little project I am working on. My compiler (GCC) is finding a particular construct ambiguous. Can anyone suggest something I might change in the declaration of class Length so that I can use operator+ the way I'd like? //========================================= // File lentest.h: #ifndef FDIMENS_LENGTH_INCL #define FDIMENS_LENGTH_INCL
0
1822
by: Ioannis Vranos | last post by:
Although not about C++ only, I think many people here have K&R2, so I post this message in clc++ too. In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome.
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.