我正在創建一個包含檔案和目錄的容器,檔案和目錄是來自名為資源的父類的派生類,我的問題出現在嘗試使用目錄類的成員函式時,find() 在內容中搜索名稱一個目錄,這個內容可以是檔案或目錄 find() 作業得很好,但是當我嘗試在目錄內的目錄中使用它時,我得到了這個
error: ‘class sdds::Resource’ has no member named ‘find’
這是 find() 函式**錯誤來自第二個 for 回圈,同時嘗試在指向目錄的資源指標中使用 find。
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() 回傳成員的名稱,OpFlags 引數確定是否應該在目錄中查看目錄,NodeType 回傳內容的型別這是 Directory 類
class Directory: public Resource{
std::vector<Resource *> m_contents;
int ccount = 0;
public:
//stuff
};
這是資源類
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() {}
};
如您所見,Resource 沒有這樣的 find() 成員我知道,我也知道這個問題可能很難理解,對此我深表歉意,我想知道的是如何在目錄內的目錄中使用 find(),只是要注意:我不能修改 Resource 類,我的整個代碼都可以作業,但那部分。
uj5u.com熱心網友回復:
只需(*ip)->從qq = (*ip)->find(*ip->name());. find不是成員函式。
qq = find((*ip)->name());
也許應該將標志傳遞給呼叫
qq = find((*ip)->name(), ff);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/442160.html
