1

I have a class called OrderedList in a namespace MYLIB. In it I have a nested class called iterator which will be used as an iterator for the OrderedList class.

Following is the snippet of my code where I create OrderedList and iterator classes:

    template<class T>
    class OrderedList
    {
        private:
            ListNode<T>* head;
            ListNode<T>* tail;
            int total;
        public:
            OrderedList(T[],int);
            ~OrderedList();
            void insert(const T&);
            void sort(int);
            void output();
            class iterator
            {
                private:
                    ListNode<T>* curr;
                    int current;
                    OrderedList& order;
                public:
                    iterator(OrderedList& ord, bool is_end)
                    {
                        this->order = ord; //problem is here
                        if(is_end == false)
                        {...

Snippet of my main function:

int main()
{
    int one[5] = {9,7,5,4,1};
    MYLIB::OrderedList<int> odd(one,5);
    odd.output();

    MYLIB::OrderedList<int>::iterator starter(odd,false);

When I compile it gives me the following error:

OrderedList.cpp: In instantiation of ‘MYLIB::OrderedList<T>::iterator::iterator(MYLIB::OrderedList<T>&, bool) [with T = int]’:
OrderedList.cpp:215:53:   required from here
OrderedList.cpp:47:6: error: uninitialized reference member ‘MYLIB::OrderedList<int>::iterator::order’ [-fpermissive]
      iterator(OrderedList& ord, bool is_end)
      ^
4
  • Please paste code (text) not images. Commented May 24, 2015 at 6:22
  • 2
    Every time a picture of code is posted, somewhere a kitten dies. Please, please don't do that. Commented May 24, 2015 at 6:24
  • Lol @WhozCraig. I believe posting picture is much more clear than the text since you can refer to the line numbers as well :) Commented May 24, 2015 at 6:31
  • 1
    @FahadUrRehman And its impossible to copy/paste posted code to reproduce problems, a highly desired and to-be-considered mandetory goal of an MCVE. Leave both the line numbers and pictures at home. If there is a line of code that is causing you grief, clearly mark it with a comment. if the grief is an error message saying "line XXXX", then mark said-line with a comment to the effect // line XXXX HERE. Make it as easy as possible for people to test your problem and produce a solution. Commented May 24, 2015 at 6:39

1 Answer 1

1

In C++ a reference must be initialized in the member initialization list, it cannot be initialized in the coustructor body.

You need to change your code to

iterator(OrderList& ord, bool is_end) : order(ord) {
    if (is_end == false) {
        ...
    }
}

instead of using an assignment.

Assignment on a reference does a different thing (it assigns the original object the reference is referencing). You cannot change what a reference is bound to.

PS: Why stopping there and not going for if ((is_end == false) == true) ? ;-)

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

1 Comment

Thanks heaps it works :) Well, yes I can do that but I like my code to be spread out, so that it is clear for future reference. That notation can be unclear at times, especially if you haven't done c++ for a while and then referring back to this code after a while will be a bit hard.

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.