使用自定义比较器进行 std::list::sort 时出错(预计在 ' 之前的主表达式)' 令牌)
标题是主要问题。 确切的场景(我是“使用命名空间 std;”):
void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) {
list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator);
}
这是比较器定义:
class Substring {
// ...
class OccurrenceComparator {
public:
bool operator() (Substring * a, Substring *b);
}
};
比较器的实现直观且简单。 我还在 std::set 中使用了一个非常相似的比较器,它工作得很好。 当我添加 sortByOccurrence() 函数时,它在标题中给出了错误。
我应该怎么办?
编辑:我现在尝试传递 Substring::OccurrenceComparator() 作为比较器,并收到以下错误:
g++ -Wall -g -c substring_miner.cpp -o obj/subtring_miner.o
substring_miner.cpp: In function ‘void SubstringMiner::sortByOccurrence(std::list<Substring*, std::allocator<Substring*> >&)’:
substring_miner.cpp:113: error: no matching function for call to ‘std::list<Substring*, std::allocator<Substring*> >::sort(std::_List_iterator<Substring*>, std::_List_iterator<Substring*>, Substring::OccurrenceComparator)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Substring*, _Alloc = std::allocator<Substring*>]
make: *** [substring_miner] Error 1
我的代码行现在:
list<Substring *>::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator());
我无法删除模板,或者它给了我错误提示模板参数错误。
The title is the main question. The exact scenario (I am 'using namespace std;'):
void SubstringMiner::sortByOccurrence(list<Substring *> & substring_list) {
list::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator);
}
This is the comparator definition:
class Substring {
// ...
class OccurrenceComparator {
public:
bool operator() (Substring * a, Substring *b);
}
};
Implementation of the comparator is intuitive and trivial. I am also using a very similar comparator in a std::set and it works fine. When I add the sortByOccurrence() funcion it gives me the error in the title.
What should I do?
EDIT: I'm now trying to pass Substring::OccurrenceComparator() as the comparator, and am getting the following error:
g++ -Wall -g -c substring_miner.cpp -o obj/subtring_miner.o
substring_miner.cpp: In function ‘void SubstringMiner::sortByOccurrence(std::list<Substring*, std::allocator<Substring*> >&)’:
substring_miner.cpp:113: error: no matching function for call to ‘std::list<Substring*, std::allocator<Substring*> >::sort(std::_List_iterator<Substring*>, std::_List_iterator<Substring*>, Substring::OccurrenceComparator)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Substring*, _Alloc = std::allocator<Substring*>]
make: *** [substring_miner] Error 1
My code line is now:
list<Substring *>::sort(substring_list.begin(), substring_list.end(), Substring::OccurrenceComparator());
I can't remove the template or it gives me an error saying that template parameters were wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
list
成员sort
是一个非静态函数,因此必须在列表实例上调用。编辑:您不能使用自由函数
std::sort
,因为它需要随机访问迭代器,而list
迭代器则不需要。list
membersort
is a non-static function so must be called on a list instance.Edit: You can't use the free function
std::sort
as it requires random access iterators whichlist
iterators are not.您将一个类作为参数传递给函数。 您不能这样做 - 您必须创建该类的实例,并传递该实例:
请注意上面
OccurenceComparator
之后的额外括号,它们使用默认构造函数创建该类的临时对象。另一个错误是您将
list::sort
作为类std::list
上的静态函数调用。 它不是静态的,因此您需要将其作为substring_list
上的成员函数进行调用。You're passing a class as an argument to a function. You cannot do that - you have to create an instance of the class, and pass that:
Note the extra parentheses after
OccurenceComparator
above that create a temporary object of the class using default constructor.Another mistake is that you're calling
list::sort
as a static function on classstd::list
. It's not static, so you need to call it as a member function onsubstring_list
.原来的问题已经被 Pavel Minaev 上面解决了。
但有一些额外的注意事项。
operator() 可能应该是 const (以及参数)。
对于像这样的简单类,将它们构造成结构会更容易。
请注意,比较必须提供严格的弱排序:
The original problem was already solved by Pavel Minaev above.
But some extra notes.
The operator() should probably be const (as well as the parameters).
For simple classes like this it is easier to just make them structures.
Note the comparison must provide a strict weak ordering: