0

I've been trying to compile and have played around with the ampersands and still can't figure out what the error is. Any ideas?

qsort.cc:22:23: error: no matching function for call to ‘qsort<int>::quicksort(std::vector<int, std::allocator<int> >*)’
qsort.cc:22:23: note: candidate is:

qsort.h:16:6: note: void qsort<T>::quicksort(std::vector<T>&) [with T = int]
qsort.h:16:6: note:   no known conversion for argument 1 from ‘std::vector<int, std::allocator<int> >*’ to ‘std::vector<int, std::allocator<int> >&’

Header:

template <class T>
class qsort
{
public:

void quicksort(vector<T> &v);
void qusort(vector<T> &v, int left, int right);
void print(vector<T> &v);
};

template <class T>
void qsort<T>::quicksort(vector<T> &v)
{
qusort(&v, 0, 0);
}

template <class T>
void qsort<T>::print(vector<T> &v)
{
    for(int i = 0; i < v.size(); i++)
    {
    cout << v[i] << endl;
    }
}

Main:

int main()
{
qsort<int> asort;
vector<int> v;

v.push_back(2);
v.push_back(1);
v.push_back(7);
v.push_back(3);
v.push_back(8);
v.push_back(4);
v.push_back(0);
v.push_back(9);
v.push_back(5);
v.push_back(6);

asort.quicksort(&v);
asort.print(&v);

return 0;
}

Updated errors after ampersand removed out of main function calls (the short version)

qsort.h: In member function ‘void quisort::qusort(std::vector&, int, int) [with T = int]’: qsort.h:18:5: instantiated from ‘void quisort::quicksort(std::vector&) [with T = int]’

qsort.cc:22:22: instantiated from here qsort.h:27:38: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive] /usr/include/c++/4.6/bits/basic_string.tcc:214:5: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator]’ [-fpermissive]

qsort.h:18:5: instantiated from ‘void quisort::quicksort(std::vector&) [with T = int]’ qsort.cc:22:22: instantiated from here qsort.h:31:9: error: no match for ‘operator<’ in ‘(& v)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = int, _Alloc = std::allocator, std::vector<_Tp, _Alloc>::reference = int&, std::vector<_Tp, _Alloc>::size_type = long unsigned int](((long unsigned int)i)) < pivot’ qsort.h:31:9: note: candidates are: /usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) /usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)

1
  • Side Note: I would advise you change your class name or put it in a namespace, because the name qsort is already occupied by the standard library implementation of std::qsort.
    – WhozCraig
    Apr 22, 2013 at 0:39

1 Answer 1

1

Your member function takes the arguments by reference. They don't take pointers (that which is returned by the address operator &). You simply need to pass the object and the reference will bind:

asort.quicksort(v);
asort.print(v);
4
  • Problem is when I remove it I get a massive error block with more conversion errors. Apr 22, 2013 at 0:41
  • It should be qusort(v, 0, 0); Apr 22, 2013 at 1:10
  • @user2057191 What is the signature of qusort?
    – David G
    Apr 22, 2013 at 1:19
  • It was actually an error in type within one of my functions, but this solved a majority of my problem. Thanks! Apr 22, 2013 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.