2015-06-20 64 views
1

我試圖讓程序寫出所有字符串的排列。這裏是我的代碼:排序功能中'operator ='不匹配

#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 ++,並設置它使用C++ 11的標準。什麼似乎是問題?

回答

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方法返回什麼類型的值,我們可以簡單地通過使用google來完成。

+0

這正是問題;我認爲它會返回對象的排序版本。謝謝!一旦時限到期,我會接受你的回答。 –

+0

@VirtualDXS好吧,沒有必要xD btw,我添加了如何閱讀錯誤信息並自行找到問題的描述:D – FalconUA

+0

哦,我明白了。我不太理解「不匹配」是什麼意思。謝謝! –

3

此線路不正確,因爲std::sort返回void

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

您不能將void指定爲std::string

從該行刪除input =。這不是必需的。