不匹配'运营商='在排序功能

时间:2015-06-20 18:25:38

标签: c++ c++11 g++ codeblocks

我试图让程序写出字符串的所有排列。这是我的代码:

#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));
}

但是,我收到以下错误:

[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)))'|

我正在使用带有g ++的Code :: Blocks并将其设置为使用C ++ 11标准。什么似乎是问题?

2 个答案:

答案 0 :(得分:3)

此行格式不正确,因为std::sort返回void

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

您无法将void分配给std::string

从该行中删除input =。它不需要。

答案 1 :(得分:3)

sort方法本身会返回void,因此您尝试执行的操作是将string分配给void。 只需写下sort(begin(input), end(input))即可。

<强>更新
好吧,让我们分析一下编译器提供的错误消息:

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)))'

最后3行似乎很难理解,但最重要的是第一行:

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

这意味着编译器无法找到规则,这可以让您将input分配给右侧的内容。所以,现在,当我们已经知道问题在于赋值时,调试过程要简单得多 - 我们必须找到sort方法返回的值的类型,我们可以这样做只需使用谷歌。

相关问题