排序功能中的'operator='不匹配
No match for 'operator=' in sort function
我试图让程序写出字符串的所有排列。这是我的代码:
#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 标准。似乎有什么问题?
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 来做到这一点。
此行格式不正确,因为std::sort
返回void
。
input = sort(begin(input), end(input));
不能将void
分配给std::string
。
从该行中删除input =
。 不需要。
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- Valarray 切片数组与 'operator*' 不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 使用标准::时间::high_resolution_clock时"operator ="不匹配
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 错误:尝试减去字符串时'operator-'不匹配
- 调用模板方法会导致'operator<'不匹配
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 使用 #omp 时'operator -'不匹配
- 尝试使用 fstream 将字符写入文件:'operator<<'不匹配
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 有没有办法修复此错误:与'operator[]'不匹配(操作数类型为 'empl' 和"int")
- 错误与'operator='不匹配(操作数类型为 'Person' 和"Person*")
- 我得到"将值分配给结构数组 iin 时与'operator='不匹配C++
- 在地图中搜索值时'operator='不匹配
- 错误:'operator<<'和"运算符>>"不匹配。请帮忙?