-2

I am trying to implement a naive File System. I have this class

class BaseFile {
private:
    string name;

public:
    BaseFile(string name);
    string getName() const;
    void setName(string newName);
        bool operator < (const BaseFile& str) const;
        bool operator > (const BaseFile& str) const;

    virtual int getSize() = 0;

};

and the subclass

class Directory : public BaseFile {
private:
    vector<BaseFile*> children;
    Directory *parent;

public:
    Directory(string name, Directory *parent); r
    Directory *getParent() const; 
    void setParent(Directory *newParent); 
    void removeFile(string name); 
    void removeFile(BaseFile* file); 
    void sortByName(); 
    bool SortSize(BaseFile& a , BaseFile& b);
    void sortBySize(); 
    vector<BaseFile*> getChildren(); n
    int getSize(); 
    string getAbsolutePath();  

};

I need to sort the vector<BaseFile*> children once by the attribute size by activating the function SortBySize(). And by name by the function SortByName(). I cant use sort algorithm I want to use std::sort()

For the names I overloaded the operators < > and for the numbers(size) I want to define a custom comparison function.

I defined the function in the class Directory. I tried to make is static it didn't work

bool Directory::SortSize(BaseFile& a , BaseFile& b) 
{
    return (a.getSize() < b.getSize());
}

but when I use std::(children.begin(), children.end(), sortSize)

I get "invalid use of non-static member function". Any help?

Teivaz
  • 5,462
  • 4
  • 37
  • 75

1 Answers1

0

To sort by size using a lambda expression:

vector<BaseFile*> files;
std::sort(files.begin(), files.end(), 
          [](const BaseFile* a, const BaseFile* b)
          { return a->getSize() < b->getSize(); });
RandomBits
  • 4,194
  • 1
  • 17
  • 30