Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 04:49 PM
- Steve -
Guest
 
Posts: n/a
Default Strange Results with Overloaded Operators

I'm having problems with the code you can find at http://planetevans.com/c

Particulary with test6(). However even with the earlier tests they work
fine when run, but crash when run in the debugger. I'm also still having
problems with the destructor.


  #2  
Old July 19th, 2005, 04:49 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Strange Results with Overloaded Operators


"- Steve -" <sevans@foundation.sdsu.edu> wrote in message
news:691287bd9149eb90624f2c02e48cbb27@free.teranew s.com...[color=blue]
> I'm having problems with the code you can find at http://planetevans.com/c
>
> Particulary with test6(). However even with the earlier tests they work
> fine when run, but crash when run in the debugger. I'm also still having
> problems with the destructor.
>[/color]

Both your operator[] are wrong. Think carefully about what happens when
arrayLow is not zero.

Should have spotted this in your earlier post, sorry.

john


  #3  
Old July 19th, 2005, 04:50 PM
- Steve -
Guest
 
Posts: n/a
Default Re: Strange Results with Overloaded Operators

"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bg44j3$407em$1@ID-196037.news.uni-berlin.de...[color=blue]
> Both your operator[] are wrong. Think carefully about what happens when
> arrayLow is not zero.
>
> Should have spotted this in your earlier post, sorry.
>
> john
>
>[/color]

Okay that was a stupid mistake. Changed them to execute return
array[forLocation];

I'm still having problems though. With the destructor enabled it crashes on
test2(). Without it enabled it crashes on test3().


  #4  
Old July 19th, 2005, 04:50 PM
ES Kim
Guest
 
Posts: n/a
Default Re: Strange Results with Overloaded Operators

"- Steve -" <sevans@foundation.sdsu.edu> wrote in message
news:51ec7e9585641f362111867b59845086@free.teranew s.com...[color=blue]
> "John Harrison" <john_andronicus@hotmail.com> wrote in message
> news:bg44j3$407em$1@ID-196037.news.uni-berlin.de...[color=green]
> > Both your operator[] are wrong. Think carefully about what happens when
> > arrayLow is not zero.
> >
> > Should have spotted this in your earlier post, sorry.
> >
> > john
> >
> >[/color]
>
> Okay that was a stupid mistake. Changed them to execute return
> array[forLocation];
>
> I'm still having problems though. With the destructor enabled it crashes on
> test2(). Without it enabled it crashes on test3().
>
>[/color]

Still wrong.
What if a[-3] for IntArray a(-3, 6)?

--
ES Kim



  #5  
Old July 19th, 2005, 04:50 PM
- Steve -
Guest
 
Posts: n/a
Default Re: Strange Results with Overloaded Operators

Then it returns array[-3] which is a valid variable isn't it?


"ES Kim" <eskim@svd.co.kr> wrote in message
news:bg4a5a$kmo$1@news1.kornet.net...[color=blue]
> "- Steve -" <sevans@foundation.sdsu.edu> wrote in message
> news:51ec7e9585641f362111867b59845086@free.teranew s.com...[color=green]
> > "John Harrison" <john_andronicus@hotmail.com> wrote in message
> > news:bg44j3$407em$1@ID-196037.news.uni-berlin.de...[color=darkred]
> > > Both your operator[] are wrong. Think carefully about what happens[/color][/color][/color]
when[color=blue][color=green][color=darkred]
> > > arrayLow is not zero.
> > >
> > > Should have spotted this in your earlier post, sorry.
> > >
> > > john
> > >
> > >[/color]
> >
> > Okay that was a stupid mistake. Changed them to execute return
> > array[forLocation];
> >
> > I'm still having problems though. With the destructor enabled it[/color][/color]
crashes on[color=blue][color=green]
> > test2(). Without it enabled it crashes on test3().
> >
> >[/color]
>
> Still wrong.
> What if a[-3] for IntArray a(-3, 6)?
>
> --
> ES Kim
>
>
>[/color]


  #6  
Old July 19th, 2005, 04:50 PM
ES Kim
Guest
 
Posts: n/a
Default Re: Strange Results with Overloaded Operators

"- Steve -" <sevans@foundation.sdsu.edu> wrote in message
news:401fbda8530a9f5e734a17020e9788e6@free.teranew s.com...[color=blue]
> Then it returns array[-3] which is a valid variable isn't it?[/color]

No. Array index starts from 0. You just changed the interface
using the array. It should be

int& IntArray::operator[] (int forLocation)
{
return array[forLocation - arrayLow];
}

For a[-3], it must return array[0].

--
ES Kim


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles