1

i have following code in c++

#include <iostream>
using namespace std;

void qsort5(int a[],int n){
    int i;
    int j;
    if (n<=1)
        return;
    for (i=1;i<n;i++)
        j=0;
    if (a[i]<a[0])
        swap(++j,i,a);
    swap(0,j,a);
    qsort5(a,j);
    qsort(a+j+1,n-j-1);
}

int main()
{
    return 0;
}

void swap(int i,int j,int a[])
{
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}

i have problem

1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(16) : error C2661: 'qsort' : no overloaded function takes 2 arguments
1>Build log was saved at "file://c:\Users\dato\Documents\Visual Studio 2008\Projects\qsort5\qsort5\Debug\BuildLog.htm"

please help

2
  • Have you tried putting "void swap(..." before "void qsort5(..."? Commented Jun 10, 2010 at 18:41
  • This doesn't really have anything to do with algorithms, let alone quicksort.... Commented Sep 5, 2010 at 19:39

7 Answers 7

5

You have to declare your version of swap before you use it. Since the compiler did not see a declaration, it used the one it found in the std namespace. Also, you mispelled qsort5 (omitting the 5 in the last line of the function). Again, the compiler found a function with that name (but a different signature) in std and complained.

You should either move the entire definition of swap to a position before function qsort5 or insert a declaration

void swap(int i,int j,int a[]);

before qsort5.

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

3 Comments

yes but it show me one mistake only 1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(26) : error C2660: 'qsort' : function does not take 2 arguments
@davit-datuashvili: The standard library's qsort function requires 4 arguments. You have supplied only two.
Or more likely, you meant qsort5 not qsort.
4

swap is a function in std which must be included by <iostream>. When you attempt to make calls to your swap, it can't find it (I'll explain in a moment) and instead looks at the std::sort, which takes two arguments (hence the first error).

The reason it can't find your swap is because it is declared after it is called. You need to either move the definition of your swap above that of qsort5 or forward declare it:

void swap(int i,int j,int a[]);

void qsort5(int a[],int n){ 
  ...

That tells the compiler that your swap function exists and it will use that when you call swap with 3 arguments.

2 Comments

std::swap is in <algorithm>, not <iostream>.
Yes, but as I said, it must be included by <iostream> otherwise that error wouldn't show. There's nothing stopping those headers from including other headers.
1

do not use using namespace std;, is generally bad practice. this brings std::swap into scope, so compiler picks up that swap, rather than yours (since yours is not defined at all this point).

Move definition of your swap before you use it.

1 Comment

+1 Bringing in "using std" brings a lot of problems like this. If you know the std library good enough you catch these quickly, but using std is often done by beginners that learn to shun it later.
1

Looks like you are missing a brace:

for (i=1;i<n;i++)
    j=0;

In the above loop, j is set to zero a whole bunch of times. This can be simplified (by you and will be by the compiler) to:

j = 0;

Otherwise there is a missing set of braces or something else.

1 Comment

lol, yeah I just saw that also and was tremendously confused :)
1

Unless this is an exercise, have you considered using std::sort instead of reinventing the wheel? Then your error goes away because the qsort5 function can be removed.

Comments

0

You're expecting your swap(int,int,int) function to be called. However if you look at the error, it mentions 'void std::swap(...)'. This is because you are using namespace std, and your swap function is declared below qsort5.

So it looks for a swap function, and can only see std:swap. Try putting your swap function above qsort5 so it can see that one as well.

Comments

0

Did you perhaps intend to call qsort5 at the end, and call qsort instead?

Comments

Your Answer

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