c++ - 读取存储在多重映射中的私有(private)枚举作为值

标签 c++ c++11 enums multimap

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

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

据我所知,我的指针指向枚举类,它无法访问 getFileType();功能。

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

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

感谢您的宝贵时间!

#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的值基本上有两种不同的解决方案,具体取决于您实际想要打印的内容。

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

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

如果要打印字符串或枚举的其他表示形式,则需要重载“输出”运算符 << .也许是这样的

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而不是 map :

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;
}

关于c++ - 读取存储在多重映射中的私有(private)枚举作为值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35131956/

相关文章:

c++ - 带有 IOCTL_DISK_GET_DRIVE_GEOMETRY 的 DeviceIoControl() 失败并返回错误代码 87。为什么?

C++ 内存泄漏 new 运算符

c++ - 为什么我的输出在到达代码的这一部分时卡住了?

c++ - 如何在boost序列化中使用数组优化

c++ - 如何将 vector 元素作为参数传递给可变参数模板函数?

c# - 如何使用枚举填充组合框 - 在组合框中显示枚举的整数(值)

c++ - C++ 中的枚举类型

java - 如何在Java中将枚举转换为字节数组?

c++ - std::function 和 std::mem_fn 有什么区别

C++11 在 constexpr 函数中使用统一值初始化数组