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 12 1196
Steve wrote: 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) { .. } "
--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Steve wrote: 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
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
Steve wrote:
Steve wrote:
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
--
--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
Rakesh Kumar wrote: 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];
^
"Steve" <nospam@nopes> wrote in message
news:40********@clarion.carno.net.au Rakesh Kumar wrote: 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;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Steve wrote:
Rakesh Kumar wrote:
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
Steve wrote:
Steve wrote:
Rakesh Kumar wrote:
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.
"John Carson" <do***********@datafast.net.au> wrote in message news:<40********@usenet.per.paradox.net.au>... "Steve" <nospam@nopes> wrote in message news:40********@clarion.carno.net.au Rakesh Kumar wrote: 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.
Steve <nospam@nopes> wrote in message news:<40********@clarion.carno.net.au>... Steve wrote:
Rakesh Kumar wrote:
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
Steve <nospam@nopes> wrote: Rakesh Kumar wrote: 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::operator[].
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.
"Steve" <nospam@nopes> wrote in message
news:40********@clarion.carno.net.au...
Steve wrote:
Rakesh Kumar wrote:
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!
"ye hua" <ef*********@hotmail.com> wrote in message
news:c6**********@news1.wdf.sap-ag.de 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?"
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Terje Slettebų |
last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been
discussed before, I'd appreciate a pointer to it. I again haven't found it
in a search of the PHP groups.
The PHP...
|
by: Dave Theese |
last post by:
Hello all,
I'm trying to get a grasp of the difference between specializing a function
template and overloading it. The example below has a primary template, a
specialization and an overload. ...
|
by: |
last post by:
Hi all,
I've been using C++ for quite a while now and I've come to the point where I need to overload new
and delete inorder to track memory and probably some profiling stuff too. I know that...
|
by: zeus |
last post by:
I know function overloading is not supported in C.
I have a few questions about this:
1. Why? is it from technical reasons? if so, which?
2. why wasn't it introduced to the ANSI?
3. Is there any...
|
by: JaSeong Ju |
last post by:
I would like to overload a C function. Is there any easy way to do this?
|
by: |
last post by:
Hi,
Why can I not overload on just the return type?
Say for example.
public int blah(int x)
{
}
|
by: brzozo2 |
last post by:
Hello, this program might look abit long, but it's pretty simple and
easy to follow. What it does is read from a file, outputs the contents
to screen, and then writes them to a different file. It...
|
by: lordkain |
last post by:
is it possible to do some kind of function overloading in c? and that
the return type is different
|
by: placid |
last post by:
Hi all,
Is it possible to be able to do the following in Python?
class Test:
def __init__(self):
pass
def puts(self, str):
print str
|
by: Matthew |
last post by:
Am I correct in thinking there is no method/function overloading of any
kind in any version of PHP?
Thanks,
Matthew
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |