指针容器
我在声明指向类实例的 STL 指针集时遇到了一些麻烦。 更具体地说,我有这样的情况:
class SimulatedDiskFile {
private:
// ...
public:
// ...
struct comparator {
bool operator () (SimulatedDiskFile* const& file_1, SimulatedDiskFile* const& file_2) {
return ((*file_1)->getFileName() < (*file_2)->getFileName());
}
};
}
typedef set<SimulatedDiskFile*, SimulatedDiskFile::comparator> FileSet;
上面的代码不起作用。 编译器说它没有找到成员 SimulatedDiskFile::comparator() 函数。 如果我将函数与此声明一起放置(在结构之外),编译器会说它需要一个类型。
现在我的疑问(我猜不仅是一个,而且是相关的):
- 一组指针的正确声明是什么?
- 比较指针的比较函数的正确声明是什么?
在发布之前,我确实在很多地方查找过,但我发现这些参考文献令人困惑,并且与我的特殊情况不太相关(尽管我认为这是愚蠢的微不足道 - 实际上,也许这就是原因)。 所以,任何好的链接也有很大的帮助!
提前致谢!
I'm having some trouble in declaring a STL Set of pointers to class instances. More specifically, I have this scenario:
class SimulatedDiskFile {
private:
// ...
public:
// ...
struct comparator {
bool operator () (SimulatedDiskFile* const& file_1, SimulatedDiskFile* const& file_2) {
return ((*file_1)->getFileName() < (*file_2)->getFileName());
}
};
}
typedef set<SimulatedDiskFile*, SimulatedDiskFile::comparator> FileSet;
The code above is not working. Compiler says it didn't find a member SimulatedDiskFile::comparator() function. If I put the function with this declaration (outside the struct), compiler says it was expecting a type.
Now here com my doubts (not only one, but related, I guess):
- What is the the correct declaration for a set of pointers?
- What is the correct declaration for a comparison funcion that compares pointers?
I did look up in many places before posting, but I found the references confusing and not quite related to my special case (as stupidly trivial as I think it is - actually, maybe this is the cause). So, any good links are of great help too!
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复了一些小问题,
编译得很好。
Fixing a few glitches,
compiles just fine.
由于您没有显示“getFileName()”方法应该在哪里,所以我只是冒险假设您无意在比较器中双重取消引用指针。 即,您应该执行以下任一操作:
或:
但不能同时执行两者。
Since you aren't showing where the 'getFileName()' method is supposed to be, I'm just going to go out on a limb and assume that you don't mean to double-dereference your pointers in the comparator. ie, you should do either:
or:
but not both.