views:

211

answers:

7

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

+5  A: 

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.

Dirk
yes but it show me one mistake only1>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.
Billy ONeal
Or more likely, you meant `qsort5` not `qsort`.
Mike Seymour
A: 

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.

Cam
+1  A: 

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.

aaa
+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.
daramarak
+3  A: 

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.

Peter Alexander
`std::swap` is in `<algorithm>`, not `<iostream>`.
Billy ONeal
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.
Peter Alexander
A: 

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.

Thomas Matthews
A: 

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

EvilTeach
+1  A: 

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.

Mark B