我搜索了宇宙最远的地方(又名互联网),但没有找到任何关于如何解决我的问题的提示。所以我来找你。
我正在尝试遍历包含字符串对的列表。 该列表是数组中的 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++ 的新手。
最佳答案
first
和 second
是 std::pair
的成员变量不是成员方法。去掉括号:
std::cout << (*logit).first << std::endl << (*logit).second << std::endl;
关于c++ - 遍历对列表,列表在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16623190/