-1

I wrote the following C++ code to generate the next inverse alphabet order of the input string. However I got error saying: no matching function for call to 'reverse': candidate template ignored: deduced conflicting types for parameter '_BidirectionalIterator' ('std::__1::basic_string' vs. 'int') reverse(_BidirectionalIterator __first, _BidirectionalIterator __last). I couldn't understand the error message and don't know how to adjust it. Could anyone help me out here? Thanks!

#include<iostream>
#include<string>
using namespace std;

string gen(string A,int n){ 
    int i, j;
    for(i= n-1;(i > 0 && A[i-1]<A[i]);i--)
        ; // empty statement
    if (i == 0)
        return 0;
    for (j = i+1; j < n && A[i-1] > A[j]; j++)
        ; // empty statement

    swap(A[i-1],A[j-1]); // swap values in the two entries
    string subline =A.substr(i,n-i);
    subline=reverse(subline,n-i);
    A=A.substr(0,i-1)+subline;
    return A;
}

void swap(int &a,int &b)
{
    int temp=b;
    b=a;
    a=temp;
}

string reverse(string k,int length)
{    
    for(int m=0;m<length/2;m++)
    {
        char temp=k[length-1-m];
        k[length-1-m]=k[m];
        k[m]=temp;
    }
    return k;
}

int main(void)
{
    cout<<"Please enter a string"<<endl;
    string arrayperm;
    cin>>arrayperm;
    int length=arrayperm.length();    
    string newone=gen(arrayperm,length);
    cout<<"The new array is: "<<newone<<endl;
    return 0;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • possible duplicate of [Does function order matter in C++?](http://stackoverflow.com/questions/26122024/does-function-order-matter-in-c) – Cory Kramer Feb 16 '15 at 12:48
  • why do folk insist on rewriting `std::swap` (badly). – Bathsheba Feb 16 '15 at 12:49
  • Don't say `using namespace std;` It can cause clashes with [standard library names](http://en.cppreference.com/w/cpp/algorithm/reverse). – juanchopanza Feb 16 '15 at 12:49
  • You are `using namespace std;`, then defining `swap` and `reverse` which are functions defined in the standard library, you're going to cause problems. But more importantly, you use your `reverse` before it is defined, see my link above. – Cory Kramer Feb 16 '15 at 12:51
  • My eyes! Please, read this: [coding style by mozilla](https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style). – Piotr Siupa Feb 16 '15 at 12:52
  • sorry about the bad coding style guys,I would try to learn more and adjust. And all the answers have really been of great help! Thanks guys!!! – Shirley Zhu Feb 16 '15 at 13:10

1 Answers1

0

Your code can be made to compile if you you move your reverse function to the top of the file and nest it in a namespace, for example:

namespace oops{
    string reverse(string k, int length)
    {
        for (int m = 0; m < length / 2; m++)
        {
            char temp = k[length - 1 - m];
            k[length - 1 - m] = k[m];
            k[m] = temp;
        }
        return k;
    }
}

And then when you call it use: subline = oops::reverse(subline, n - i);

But, as currently implemented it will run but won't reverse the string. I'd recommend not reinventing the wheel:

#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main(void)
{
    string arrayperm = "Lorem ipsum";
    string newone(arrayperm.size(), '\0');

    reverse_copy(arrayperm.begin(), arrayperm.end(), newone.begin());

    cout << "The new array is: " << newone << endl;
    return 0;
}
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288