Overloading []

S

Steve

Hi,

I have two classes: pair, and Array. In Array, I have overloaded the
subcript operator like this:

pair& operator[] (int index) {
return pair(index);
}

However, it results in a compilation error saying that there's not
suitable funtion that returns a 'pair' when I do:

pair a = myArray[0];

It seems to be always returning a reference of type Array. How can I
make it return another type? Thanks,

Steve
 
R

Rakesh

Steve said:
Hi,

I have two classes: pair, and Array. In Array, I have overloaded the
subcript operator like this:

pair& operator[] (int index) {
return pair(index);
}
It seems to be always returning a reference of type Array. How can I
make it return another type? Thanks,\

Your function declaration ( and hence, the definition) says that .
Try to look at the return type of the method - "pair& operator[] (int
index) { .. } "
 
S

Steve

Steve said:
Hi,

I have two classes: pair, and Array. In Array, I have overloaded the
subcript operator like this:

pair& operator[] (int index) {
return pair(index);
}

I know this is syntactically incorrect but I'm doing something more
complicated than this and it does return a valid reference to a valid
'pair' object, and not just a temporary as show above. However, my
question is only this: how can I make it return another type?

Steve
 
R

Rakesh Kumar

The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair &'
to 'pair'.

HTH
Hi,

I have two classes: pair, and Array. In Array, I have overloaded the
subcript operator like this:

pair& operator[] (int index) {
return pair(index);
}


I know this is syntactically incorrect but I'm doing something more
complicated than this and it does return a valid reference to a valid
'pair' object, and not just a temporary as show above. However, my
question is only this: how can I make it return another type?

Steve
 
S

Steve

Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair &'
to 'pair'.

HTH

But it should still not tell me that it can't initialize a value of type
Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not const-qualified)
cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^
 
J

John Carson

Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair
&' to 'pair'.


Nonsense. It is perfectly permissible to assign a reference to pair to a
pair.

But it should still not tell me that it can't initialize a value of
type Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not
const-qualified) cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^


The following code works fine (it uses the standard pair, but this makes no
difference --- my earlier version didn't). You need to show a compileable
example of your code so we can see what is going wrong.


#include <utility>
using namespace std;

class Array
{
pair<int,int> p[3];
public:
Array()
{
p[0].first = p[0].second = 10;
p[1].first = p[1].second = 20;
p[2].first = p[2].second = 30;
}
pair<int,int> & operator[](int index)
{
return p[index];
}
};

int main ()
{
Array a;
pair<int,int> p = a[1];
p = a[0];
return 0;
}
 
S

Steve

Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair &'
to 'pair'.

HTH


But it should still not tell me that it can't initialize a value of type
Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not const-qualified)
cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^

I know!! I'm also overloading the "=" operator and so it calls that
before calling the [] operator! darn
 
J

Jeff Schwab

Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair
&' to 'pair'.

HTH



But it should still not tell me that it can't initialize a value of
type Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not
const-qualified) cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^

I know!! I'm also overloading the "=" operator and so it calls that
before calling the [] operator! darn

No, it doesn't. I'll bet you a dollar to a doughnut you're returning a
reference to a temporary.
 
D

Dave Moore

John Carson said:
Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair
&' to 'pair'.


Nonsense. It is perfectly permissible to assign a reference to pair to a
pair.

Well .. only if there is a copy constructor defined for class pair (
at least for copy initialization, as specified in the code above and
by the OP). Admittedly, it should be generated automatically, even if
omitted from the class, but the OP *might* have done something like
declare the copy constructor private for some reason. That would then
generate a compiler error for the line in question, but I doubt it
would be the message he actually received.
 
D

Dave Moore

Steve said:
Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair &'
to 'pair'.

HTH


But it should still not tell me that it can't initialize a value of type
Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not const-qualified)
cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^

I know!! I'm also overloading the "=" operator and so it calls that
before calling the [] operator! darn

Sorry, but no ... operator[] has higher precedence than operator=. We
can't diagnose your problem much further without some more code. You
say you are overloading both the subscripting and assignment operators
... as shown in other examples in this thread, this can (and should)
work just fine. Just post the relevant parts of the declarations of
your pair and Array classes and someone will probably be able to spot
the error.

HTH, Dave Moore
 
O

Old Wolf

Steve said:
Rakesh said:
if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.
I still get this:
app.cpp(121): error: a reference of type "pair &" (not const-qualified)
cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^

The compiler is saying that the type of "myArray[0]" is "Array".
You have not posted the declaration of "myArray", or the exact
declaration of Array::eek:perator[].
My money is on "myArray" being a pointer to Array (hence myArray[0]
is an Array). Another possibility would be that it is an Array,
and you had declared operator[] to return an Array.

If you POST YOUR ACTUAL CODE then you will get an accurate answer.
Scraps of stuff that is a little bit like your actual code, are next to
useless.
I know!! I'm also overloading the "=" operator and so it calls that
before calling the [] operator! darn

1) operator= is not called for the expression
T p = expr;
"expr" is converted to type T, using whatever conversions are
defined, and then T's copy-constructor is called.
2) If T is a reference type (in your example it is "pair &"), then
operator= cannot be overloaded anyway.
 
Y

ye hua

Steve said:
Rakesh said:
The rule is -

if you have function signature like this -
"pair& operator[] (int )"

pair & ref = MyObj[3]; // MyObj.operator[3]
// Perfectly fine.

pair ref = MyObj[3];
// Wrong !! Since you are assigning a variable of type 'pair &'
to 'pair'.

HTH


But it should still not tell me that it can't initialize a value of type
Array..:

I still get this:
app.cpp(121): error: a reference of type "pair &" (not const-qualified)
cannot be initialized with a value of type "Array"
pair &p = myArray[0];
^

I know!! I'm also overloading the "=" operator and so it calls that
before calling the [] operator! darn
hi , what i want to say is that even your function return
a pair , you can not use it correctly , because reference to
a local variable is dangerous!
 
J

John Carson

ye hua said:
hi , what i want to say is that even your function return
a pair , you can not use it correctly , because reference to
a local variable is dangerous!

See Steve's second post, where he says:

"I know this is syntactically incorrect but I'm doing something more
complicated than this and it does return a valid reference to a valid
'pair' object, and not just a temporary as show above. However, my
question is only this: how can I make it return another type?"
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top