By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,629 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,629 IT Pros & Developers. It's quick & easy.

ambiguous superclass functions when specializing STL class

P: n/a
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<_Ty>::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<_Ty>::at(unsigned 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<_Ty>::at(unsigned 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
Share this Question
Share on Google+
2 Replies


P: n/a
pv********@yahoo.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<_Ty>::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<_Ty>::at(unsigned 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<_Ty>::at(unsigned 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

P: n/a
pv********@yahoo.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(LongVector * lv) : d_longvector(lv) {}
};
};

--
OU
May 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.