templates, inheritance and [] operator overloading

J

Joachim

Hello,
I have been trying to get the following code to work, but unfortunately
without any succes so far. I have searched in newsgroups and google but
found nothing there that could solve my problem.
Given the following classes :

template <class T>
class AAA{
int tabel[5];
public:
T &getItem(int i){
return tabel;
}
};

template <class T>
class A{
public:
AAA<T> t;
T &operator[](int index) {
return t.getItem(index);
}

};

class B : public A<int> {
public:
void Bfun(){
t[0]=4;
}
};

My gcc compiler comes with the following error:

main.cc27 : error: no match for 'operator[]' in
'((B*)this)->B::<anonymous>.A<int>::t[0]'


Is there anybody who could give me a hint in the right direction? I
tried nonmember functions (for the operator) but the compiler says that
operator[] should be a nonstatic member function.

Thanks in advance,

Joachim
 
I

Ian Collins

Joachim said:
Hello,
I have been trying to get the following code to work, but unfortunately
without any succes so far. I have searched in newsgroups and google but
found nothing there that could solve my problem.
Given the following classes :

template <class T>
class AAA{
int tabel[5];
public:
T &getItem(int i){
return tabel;
}
};

template <class T>
class A{
public:
AAA<T> t;
T &operator[](int index) {
return t.getItem(index);
}

};

class B : public A<int> {
public:
void Bfun(){
t[0]=4;
}
};

My gcc compiler comes with the following error:

main.cc27 : error: no match for 'operator[]' in
'((B*)this)->B::<anonymous>.A<int>::t[0]'

That's because AAA lacks an operator[]. try something like:

template <class T>
class AAA{
int tabel[5];
public:
T &getItem(int i){
return tabel;
}
int& operator[]( unsigned n ) { return tabel[5]; }
};
 
S

Salt_Peter

Hello,
I have been trying to get the following code to work, but unfortunately
without any succes so far. I have searched in newsgroups and google but
found nothing there that could solve my problem.
Given the following classes :

template <class T>
class AAA{
int tabel[5];

T tabel[5];
public:
T &getItem(int i){

T& getItem( const size_t i ) {
return tabel;
}
};

template <class T>
class A{
public:
AAA<T> t;
T &operator[](int index) {


T& operator[](const size_t index) {
return t.getItem(index);
}

};

class B : public A<int> {
public:
void Bfun(){
t[0]=4;

operator[](0) = 4;
or
(*this)[0] = 4;
}

};

My gcc compiler comes with the following error:

main.cc27 : error: no match for 'operator[]' in
'((B*)this)->B::<anonymous>.A<int>::t[0]'

Is there anybody who could give me a hint in the right direction? I
tried nonmember functions (for the operator) but the compiler says that
operator[] should be a nonstatic member function.

Thanks in advance,

Joachim
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top