0

I'm overloading the subscript operator for the first time and I'm having troubles in returning a reference value.
I followed the rules of thumb from a post in c++faq tag, but there's something I'm missing.

const T& operator[](int index) const {
    if ((index < 0) || (index > size)) {
                    // provide exception handling for this error
        std::cout << "Error! Index out of bound." << std::endl;
        std::exit(0);
    } else {
        Block* b = head;
        while (b) {
            if (b->position == index)
                return *b->data;

            b = b->next;
        }
    }
}

I implemented them in both variants: with const return value and const function (as above), and without (that is identical except for the two const keywords).
The problem is that when I run the test main, it simply crashes. I thought that the bug was in the return *b->data; statement, but I can't figure neither which it could be nor if I'm wrong and there are other errors.
Any ideas?
Thanks in advance.

5
  • 3
    First, (index > size) should be (index >= size) (or the more "visual" (size <= index)), because I guess that the valid range for index is [0, size-1] (i.e. [0, size)). Second, what if the execution never meets the condition (b->position == index) and exits the loop when b becomes null? Nothing will be returned by the function and that's undefined behavior, you should handle that error case too. Commented Jun 5, 2013 at 15:27
  • @gx_ Do you have any suggestion to handle the case of no returning value? I already considered the error, but leaved it for later fix. Commented Jun 5, 2013 at 15:47
  • 1
    I don't know your code's internals... If that should logically never happen (i.e. if for each valid index there's supposed to exist an equal position in a Block of your (what seems to be a) linked-list) I suppose that you can put an assertion (or print an error and exit like above) after the while body (or just before the end of the function body). After you do that, plus the fix from my first comment, does your program still crash? and/or does it print some error message? Commented Jun 5, 2013 at 16:25
  • @gx_ Yes, I made your fixes but the program still crashes without any compiler error/warning and without print nothing. Here the code if you have the patience to read it: pastebin.com/R6yrfha0. Anyway, thanks for the help you gave me 'till now.. Commented Jun 5, 2013 at 17:35
  • 1
    Erm, why not just use std::vector? There's so much to say about your pastebin (BTW you don't show your main)... A lot of pointers (with a new but no delete); head is left uninitialized with Array's default constructor (probable cause of crash if you use head afterwards); Block's constructor wants a T* pointer but you pass it a T value (I thought it wouldn't compile but e.g. with Array<int> it passes int() i.e. 0 which is convertible to a (null) pointer, other probable cause of crash if you try *data); copy & destruction aren't handled --running out of characters Commented Jun 6, 2013 at 7:09

1 Answer 1

1

If you want to return a reference on data, I'm not sure if it's what you want, you to return a reference of type T and I'm assuming data is of type T, it should be something like:

return b->data;

Else, you are returning a reference on the adress of data.

EDIT: Corrected a mistake

Sign up to request clarification or add additional context in comments.

2 Comments

What you are saying is contradicted by the link. -> has higher precedence than *.
If I remove the * from the return, I get back this error: error: invalid initialization of reference of type 'int&' from expression of type 'int*'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.