Dessa Simpson
Dessa Simpson

Reputation: 1281

No match for 'operator=' in sort function

I'm trying to have a program write out all permutations of a string. Here is my code:

#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

int main () {
    string input;
    int length;
    cin >> input;
    input = sort(begin(input), end(input));
    length = input.length();
    do {
        cout << input;
        cout << "\n";
    } while (next_permutation(input,input+length));
}

However, I get the following error:

[path removed]\PermutIO.cpp|12|error: no match for 'operator=' in 'input = std::sort<__gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >(std::begin<std::basic_string<char> >((* & input)), std::end<std::basic_string<char> >((* & input)))'|

I am using Code::Blocks with g++ and have set it to use the C++11 standard. What seems to be the problem?

Upvotes: 1

Views: 1235

Answers (2)

Chan Kha Vu
Chan Kha Vu

Reputation: 10400

The sort method itself returns a void, so what you're trying to do is assigning string to void. Just write sort(begin(input), end(input)) instead.

Update:
Well, let's analyze the error message that your compiler gives:

error: no match for 'operator=' in 'input = 
std::sort<__gnu_cxx::__normal_iterator<char*, std::basic_string<char> > >
(std::begin<std::basic_string<char> >((* & input)), 
std::end<std::basic_string<char> >((* & input)))'

The last 3 lines might seem difficult and hard to understand, but the most important thing here is the first line:

no match for 'operator=' in 'input = ...

That means that the compiler can't find a rule, that can allow you to assign the input to something in the right side. So, now, when we've already know that the problem is in the assigning value, the debugging proccess is much simpler - we have to find what type of value does the sort method returns, we can do that simply by using google.

Upvotes: 3

Drew Dormann
Drew Dormann

Reputation: 63755

This line is malformed because std::sort returns void.

input = sort(begin(input), end(input));

You can not assign void to a std::string.

Remove input = from that line. It's not needed.

Upvotes: 3

Related Questions