2

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!

1
  • Compiles for me (on g++), except that you have an error in your comparison because you're both dereferencing the pointers and then also using indirection. Commented Jun 16, 2009 at 2:44

2 Answers 2

5

Fixing a few glitches,

#include <set>

class SimulatedDiskFile {
  public:
    int getFileName() { return 23; }

    struct comparator {
      bool operator () (SimulatedDiskFile* file_1, SimulatedDiskFile* file_2) {
        return (file_1->getFileName() < file_2->getFileName());
      }
    };
};

typedef std::set<SimulatedDiskFile*, SimulatedDiskFile::comparator> FileSet;

compiles just fine.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried changing the syntax to get it in this way. Still doens't compile, exact message is 'g++ -Wall -g -c modules/sources/model.cpp -o obj/model.o modules/sources/model.cpp:56: error: no ‘bool SimulatedDiskFile::comparator(SimulatedDiskFile* const&, SimulatedDiskFile* const&)’ member function declared in class ‘SimulatedDiskFile’'
@Rafael: This sample compiles just fine for me too. Maybe you could post a more complete segment of your code that actually reproduces the specific error?
Yes @Rafael, like @goldPseudo says -- your exact command line on this exact snippet I gave (copied and pasted into a file) compiles happily (with several g++ versions, too). Please do so copy and paste it and try to confirm you don't have a broken g++;-), then edit your answer to show a minimal complete failing case please!
1

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:

return (file_1->getFileName() < file_2->getFileName());

or:

return ((*file_1).getFileName() < (*file_2).getFileName());

but not both.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.