ambiguous superclass functions when specializing STL class

P

pvl_google

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 !
 
V

Victor Bazarov

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
/* 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
 
O

Obnoxious User

(e-mail address removed) 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) {}
};
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top