0

I'm writing my own C++ vector class and I want to be able to pass an iterator as a function parameter to my reverse() function, so that I can do something like

vector<int> scores{5,8,89,32};
reverse(scores.begin(), scores.end());

instead of

...
scores.reverse(); 
or 
reverse(scores)

I'm able to do the later with this code

template<typename T>
void reverse(vector<T> &orig) {
    //Reverse the elements of the array
    auto begin = orig.begin();
    auto end = --orig.end();    //Move the iterator 1 place back to keep within range

    while (begin < end) {

    std::swap(*begin, *end);

    begin++;
    end--;
    }
}

But when I do this

template<typename T>
void reverse(vector<T>::iterator begin, vector<T>::iterator end) {

    while (begin < end) {

        std::swap(*begin, *end);

        begin++;
        end--;
    }
}

I get this error

error: expected ')' before 'begin'

What am I doing wrong, please?

Thank you.

delivite
  • 23
  • 2
  • 7
  • I stumbled into some more issues: 1. Even with the fix of @NutCracker I got complaints because the type deduction didn't work as expected. After cheating a bit to skip the deduction... 2. Your `reverse()` has U.B. You access `end` but `end` may be one past end of storage. So, I fixed the iteration in loop. This is my fixed sample: [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/f78dbc91d86dd2ad). (However, @NutCracker nailed the primary issue of your question. Hence, I don't add my findings as answer.) – Scheff's Cat Feb 16 '20 at 10:10
  • Thanks a lot. I was supposed to add the template type when I call the reverse function. I did not. Just did and it worked after I added @NutCracker suggestion. It works now. – delivite Feb 16 '20 at 10:27

1 Answers1

3

It should be:

template <typename T>
void reverse(typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end)
{...}

Without typename compiler cannot determine whether iterator is a member or type. So compiler assumes it is a member.

NutCracker
  • 11,485
  • 4
  • 44
  • 68
  • Thank you so much. I just did that and got this error: ..\main.cpp:37:39: error: no matching function for call to 'reverse(my::vector::iterator, my::vector::iterator)' my::reverse(scores.begin(), scores.end()); ^ ..\bucket.h:230:6: note: template argument deduction/substitution failed: ..\main.cpp:37:39: note: couldn't deduce template parameter 'T' my::reverse(scores.begin(), scores.end()); – delivite Feb 16 '20 at 10:15
  • @delivite I suggest you remove `using namespace std;` from your code. And add `std::` in front of everything that comes from the standard namespace. Let me know if this helped you. – NutCracker Feb 16 '20 at 10:17
  • I don't have using namespace std; in my code. I put my class in my:: namespace. That explains my::vector – delivite Feb 16 '20 at 10:22
  • I just found the problem for this last error. Thanks a lot. It works now. I've checked the green mark for this post. Thanks again. – delivite Feb 16 '20 at 10:27
  • @delivite glad I could help – NutCracker Feb 16 '20 at 10:29