473,465 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[] overload

fox
Hi
I've two classes

class node {
....
node operator[](int idx);
}

and
class grid {
.....
node operator[](int idx);
}

but when i do :
grid *one=new grid();
cout << grid[1][1]
it uses operator [] for grid class, (which returns a node )
and doesn't use an overloaded one for the returned node value;
Is there a way to force him ?
or do i do sth wrong ?

Cheers
Jul 22 '05 #1
4 1258
fox wrote:
Hi
I've two classes

class node {
...
node operator[](int idx);
}

and
class grid {
....
node operator[](int idx);
}

but when i do :
grid *one=new grid();
cout << grid[1][1]
it uses operator [] for grid class, (which returns a node )
and doesn't use an overloaded one for the returned node value;
Is there a way to force him ?
or do i do sth wrong ?


How do you know it's wrong? Did you overload operator << (
std::ostream&, node const& )? Is indexing a node really supposed to
give another node?

Please post a small program to show the problem. Someone here probably
can show you what has gone wrong, but it is very difficult to tell from
the pseudocode.

-Jeff

Jul 22 '05 #2

"fox" <ar***@vdi.pl> wrote in message
news:bs**********@nemesis.news.tpi.pl...
Hi
I've two classes

class node {
...
node operator[](int idx);
}

and
class grid {
....
node operator[](int idx);
}

but when i do :
grid *one=new grid();
cout << grid[1][1] ^^^^^^^^^^^^^^^^^^^^^^^^^^ that could not possibly compile
if you meant cout << one[1][1];
then you have some problems here:
1. you allocated only 1 grid element so one[1] is past the array boundaries.
it should have been one[0][1]

2. one is a pointer and when someone does one[n] it actually gets *(one + n)

so what you should have done is:

grid myGrid;
// put here code to fill the collection of nodes
// then do:

cout << grid[1][1];

assuming everything else is working
it uses operator [] for grid class, (which returns a node )
and doesn't use an overloaded one for the returned node value;
Is there a way to force him ?
or do i do sth wrong ?

Cheers


Dan
Jul 22 '05 #3
err...

"Dan Cernat" <ce****@dan.com> wrote in message
news:vv************@corp.supernews.com...
so what you should have done is:

grid myGrid;
// put here code to fill the collection of nodes
// then do:

cout << grid[1][1]; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^make it
cout << myGrid[1][1];
Dan

Dan
Jul 22 '05 #4
On Tue, 30 Dec 2003 13:40:34 +0100, fox wrote:
Hi
I've two classes

class node {
...
node operator[](int idx);
}

and
class grid {
....
node operator[](int idx);
}

but when i do :
grid *one=new grid();
cout << grid[1][1]


cout << (*grid)[1][1];

You want to act on the contents of the pointer, not the pointer itself.

HTH,
M4

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
9
by: Mario Charest | last post by:
.. Hi, It's possible to overload the operator, but I wonder if it's possible to somehow overload and so forth. My goal would be to switch from static array declaration into something...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
10
by: shachar | last post by:
hi all. can i OverLoad Operators - such as Exclamation Mark "!" ? how?
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.