0

I was writing a templated version of the quicksort algorithm for various Containers utilizing iterators, but the following errors made themselves known when I attempted to construct an Object::iterator.

  • error C2061 : syntax error : identifier 'iterator'

  • error C2780: 'void myQuickSort(Container &)' : expects 1 arguments - 3 (I believe this error is a result of error C2061)

How can I create an iterator to my templated class Container without the compiler giving me these errors.

My code is written below:

#include<iostream>
#include<vector>
#include<stack>
#include<list>
#include<algorithm>

template<class Itr2Printable>
void printVec(Itr2Printable begin, Itr2Printable end)
{
    // couts containing  Objecorder
    std::cout << "[ ";

    for (begin; begin != end; ++begin)
        std::cout << *begin << " ";


    std::cout << "]";
}

template<class Container>
void myQuickSort(Container& c, Container::iterator begin, Container::iterator end)
{
    Container::iterator mid = begin + distance(begin, end) / 2;

    // inclomplete
}


int main()
{
    std::vector<int> vec{ 15, 75, 50, 0, 100, 5 };

    std::cout << "\n\n\tquickSort(";
    printVec(vec.begin(), vec.end());
    std::cout << ", " << 0 << ", " << vec.size() - 1 << ")\n";

    myQuickSort(vec, vec.begin(), vec.end());
}

1 Answers1

0

Very known issue. It is a duplicate, but it is easier for me to type the answer than to look for the duplicate question. Replace

void myQuickSort(Container& c, Container::iterator begin, Container::iterator end)

with

void myQuickSort(Container& c, typename Container::iterator begin, typename Container::iterator end)

On a side note, design of your quick sort function is incorrect. If you are expressing your function in term of iterators, do not request the container to be passed to function as well.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • Passing the container by reference was a brain fart. . If all that is required it a typename identifier, then: . 1) Why not simply write this? . template . void myQuickSort(ContainerItr begin, ContainerItr end) – user3753669 Oct 28 '15 at 20:55
  • 2) Is there a benefit to using typename "Container::iterator" instead? . template void myQuickSort(typename Container::iterator begin, typename Container::iterator end) – user3753669 Oct 28 '15 at 20:55
  • No benefit whatsoever :) Look into prototype of `std::sort`. – SergeyA Oct 28 '15 at 21:21