读取存储在多重映射中的私有枚举作为值

Reading private enums that are stored in a multimap as value

我在访问和打印枚举 class 的值时遇到了一些问题,该枚举存储在多重映射中,作为值。我得到的错误是:

error C2228: left of '.getFileType' must have class/struct/union

据我了解,我的指针指向枚举 class,它无法访问 getFileType();功能。

我可以做些什么来访问和读取枚举的值(这样我就可以在控制台上打印出来)吗?我的多图对一定和我做的一样。

非常感谢任何建议,因为我真的无法解决这个问题。

感谢您的宝贵时间!

#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <fstream>

using namespace std;

fstream fp;

class CFile {
    string m_strFile;
    unsigned int m_size;
public:
    CFile () { m_strFile = ""; m_size = 0; }
    CFile (string name, int size ) { m_strFile = name; m_size = size; }
    string getFileName () const { return m_strFile; }
    int getFileSize () const { return m_size; }
    void setFileSize ( int size ) { m_size = size; }
    bool operator< (CFile& obj) {
        return ( m_size < obj.m_size );
    }
    bool operator== (const CFile& obj) {
        return ( m_size == obj.m_size );
    }
    friend ostream& operator<< ( ostream& ost, const CFile& obj ) {
        return ost << obj.m_strFile << obj.m_size;
    }
    friend istream& operator>> ( istream& ist, CFile& obj ) {
        return ist >> obj.m_strFile >> obj.m_size;
    }
    static bool Greater(const CFile& obj1, const CFile& obj2) {
        if ( obj1.m_size > obj2.m_size )
            return true;
        else 
            return false;
    }
};

bool operator< (CFile obj1, CFile obj2) {
    return obj1.getFileName()<obj2.getFileName();
}

class CDirectory {
    string m_strDirectory;
    enum class Filetype {
        Archive, Hidden, ReadOnly, System, FileNotSupported
    };
    Filetype filetype;
    multimap <CFile, Filetype> m_DirectoryMap;
public:
    Filetype getFileType();
    CDirectory (string n) {
              fp.open (n, ios::in);
              string dirName, fileName,  fType;
              int fileSize;
              fp >> dirName;
              m_strDirectory = dirName;
              if (fType == "Archive")
                  filetype = Filetype::Archive;
              else if (fType == "Hidden")
                  filetype = Filetype::Hidden;
              else if (fType == "ReadOnly")
                  filetype = Filetype::ReadOnly;
              else if (fType == "System")
                  filetype = Filetype::System;
              else 
                  filetype = Filetype::FileNotSupported;
              while (fp >> fileName >> fileSize >> fType) {
                      CFile obj (fileName, fileSize);
                      m_DirectoryMap.insert(pair<CFile, Filetype>(CFile(obj.getFileName(), obj.getFileSize()), Filetype(filetype)));
              }
              multimap<CFile, Filetype>::iterator p = m_DirectoryMap.begin();
              while ( p != m_DirectoryMap.end()) {
                cout << endl << p->first.getFileName() << '\t' << p->first.getFileName() << '\t' << p->second.getFileType() << endl; // im getting the error here, at p->second.getFileType()
              }
    }
    string getDirectory () { return m_strDirectory; }
};

CDirectory::Filetype CDirectory::getFileType() {
        return filetype;
}

int main () {
    CDirectory obj("test.txt");
    system("pause");
    return 0;
}
p->second.getFileType()

此处访问的地图似乎是:

multimap <CFile, Filetype> m_DirectoryMap;

映射中的第二个组件是 Filetype,它是一个枚举 POD。它不是某种 class 或实现 () 运算符的函数。

From what I understand, my pointer is pointing at the enum class and it can't access the getFileType(); function.

这不是指针。它是一个迭代器。但是,是的,该错误确实表明您无法访问 getFileType 成员函数。你的枚举没有这样的成员函数,事实上,枚举不能有成员函数。

Can I do something to access and read the value of the enum

是的。对于迭代器,值在 p->second.

(so I can print it out on the console)

您需要为 operator<<(std::ostream, const FileType&) 定义重载,以便将值传递给流。

如果您想打印出 p->second 的值,基本上有两种不同的解决方案,具体取决于您实际想要打印的内容。

如果你想打印枚举的数值值你可以使用static_cast,例如

std::cout << static_cast<int>(p->second);

如果要打印字符串或枚举的其他表示形式,则需要重载 "output" 运算符 <<。也许是这样的

class CDirectory {
    ...

    friend std::ostream& operator<<(std::ostream& os, FileType const type)
    {
        static std::map<FileType, std::string> const types = {
            { FileType::Archive, "archive" },
            { FileType::Hidden, "hidden" },
            { FileType::ReadOnly, "read-only" },
            { FileType::System, "system" },
            { FileType::FileNotSupported, "not-supported" }
        };
        return os << types[type];
    }

    ...
};

那么你可以简单地做

std::cout << p->second;

使用 switch 而不是地图:

friend std::ostream& operator<<(std::ostream& os, FileType const type)
{
    switch (type)
    {
    case FileType::Archive:
        os << "archive";
        break;
    case FileType::Hidden:
        os << "hidden";
        break;
    case FileType::ReadOnly
        os << "read-only";
        break;
    case FileType::System
        os << "system";
        break;
    case FileType::FileNotSupported
        os << "not-supported";
        break;
    }

    return os;
}