Home > Software design >  Error while trying to use a member class in a vector
Error while trying to use a member class in a vector

Time:03-13

I'm creating a container with files and directories, file and directory are a derived class from a parent class called resource, my problem appears while trying to use a member function of the directory class, find() searches for a name in the contents of a directory, this contents can either be file or directory find() works just fine when but when I try to use it in a directory inside a directory I get this

error: ‘class sdds::Resource’ has no member named ‘find’ 

here is the find() function ** the error comes from the second for loop while trying to use find in a Resource pointer to a directory.

Resource* find(const std::string& know, const std::vector<OpFlags>& ff =std::vector<OpFlags>()) {
    auto result = std::find(begin(ff), end(ff), OpFlags::RECURSIVE);
    std::string wh = "";
    Resource* qq = nullptr;
    if(result == std::end(ff)){
     for (auto it = std::begin (m_contents); it != std::end (m_contents);   it) {
    
            if((*it)->name() == know){
        return *it;
        }
    }
    return nullptr;
    }
    
    else{
    for (auto ip = std::begin (m_contents); ip != std::end (m_contents) || qq != nullptr;   ip) {
        
        while((*ip)->type()==NodeType::FILE){
          ip;
        }   
    qq = (*ip)->find(*ip->name());
    }   
    }
    return qq;  
    }

name () returns the name of the member, the OpFlags argument determines whether or not it should look inside the directories inside the directory, NodeType returns the type of the content this is the Directory class

class Directory: public Resource{
        std::vector<Resource *> m_contents;
    int ccount = 0;
    public:
//stuff
};

this is the Resource class

class Resource {
    protected:
        // Stores the name of the resource
        std::string m_name{};
        // Stores the absolute path of the folder where the resource is located
        std::string m_parent_path = "/";

    public:
        virtual void update_parent_path(const std::string&) = 0;
        virtual std::string name() const = 0;
        virtual int count() const = 0;
        virtual std::string path() const = 0;
        virtual size_t size() const = 0;
        virtual NodeType type() const = 0;
        virtual ~Resource() {}
    };

as you can see Resource has no such find() member I know that, I also know this question might be hard to understand and I apologize for that, what I want to know is how to use find() in directories inside the directory, just to note: I can not modify the Resource class, the entirety of my code works but that part.

CodePudding user response:

Just remove (*ip)-> from qq = (*ip)->find(*ip->name());. find is not a member function.

qq = find((*ip)->name());

Perhaps the flags should be passed to the call

qq = find((*ip)->name(), ff);
  • Related