c++ - 遍历对列表,列表在数组中

标签 c++ stdstring std-pair stdlist

我搜索了宇宙最远的地方(又名互联网),但没有找到任何关于如何解决我的问题的提示。所以我来找你。

我正在尝试遍历包含字符串对的列表。 该列表是数组中的 20 个列表之一。 这是我当前的代码:

日志记录.h:

#ifndef LOGGING_H
#define LOGGING_H
#include <iostream>
#include <list>
#include <string>

class logging
{
    public:
        void log(int,std::string,std::string);
        void draw();
        logging();
        virtual ~logging();
    private:
        int displaylevel=0;
        std::list<std::pair<std::string,std::string>> logd[20];
};

#endif // LOGGING_H

日志记录.cpp:

#include "logging.h"
#include <list>
#include <string>
#include <iostream>

logging::logging(){
    //for future use
}

void logging::log(int level,std::string category, std::string entry) {
    int thislevel;
    for (thislevel=level-1;(thislevel>-1);thislevel--){
            std::pair <std::string,std::string> newentry;
            newentry = std::make_pair (category,entry);
            logd[thislevel].push_front(newentry);
    }
}
void logging::draw(){
    //draw console on the screen using opengl
    std::list<std::pair<std::string,std::string>>* log = &(logd[displaylevel]);
    std::list<std::pair<std::string,std::string>>::iterator logit;
    for ( logit = (*log).begin() ; logit != (*log).end() ; logit++ ) {
            std::cout << (*logit).first() << std::endl << (*logit).second() << std::endl;
    }
}

logging::~logging() {
    //Deconstructor for log class (save log to file?)
}

想法是,如果记录了重要性为 5 的事件,则将其放入列表 0、1、2、3 和 4。这样可以在游戏中显示各种详细级别(如果控制台/日志打开) 通过简单地显示对应于该详细级别(由 displaylevel 定义)的列表。但是我似乎无法正确地遍历列表,它一直抛出不匹配调用 std::basic_string 错误。感谢任何帮助,我是 C++ 的新手。

最佳答案

firstsecondstd::pair 的成员变量不是成员方法。去掉括号:

std::cout << (*logit).first << std::endl << (*logit).second << std::endl;

关于c++ - 遍历对列表,列表在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16623190/

相关文章:

c++ - QML Material Design 颜色不准确

c++ - 在Visual Studio Code中,为什么它会在类之外自动创建 “main::main and main::~main”?

c++ - 如何将 std::getline 转换为模板类型?

c++ - 是否有意使用 std::pair 进行潜在危险的隐式转换?有没有办法避免它们?

c++ - 为什么我不能使用 make_pair 来绑定(bind)?

C++ Builder XE5 链接器错误 LME1641

c++ - 待定:并行查找第一个元素

c++ - 使用 copy_if 复制 std::string(到另一个字符串)

c++ - 不带 'GetString()' 的 CString 到 std::string 的转换

c++ - 初始化 vector 对