문제

I have searched the farthest reaches of the universe (aka the internet) and have not found a single hint as to how to solve my problem. So I come to you.

I am trying to iterate over a list that contains pairs of strings. This list is one of 20 inside an array. this is my current code:

logging.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

logging.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?)
}

The idea is that if an event of importance 5 is logged, then it gets put in list 0,1,2,3 and 4. that way various verbose levels can be shown in game (if the console/log is open) by simply displaying the list corresponding to that verbose level (defined by displaylevel). however I cant seem to be able to iterate over the list properly, it keeps throwing a no match for call to std::basic_string error. Any help is appreciated, I am very new to C++.

도움이 되었습니까?

해결책

first and second are member variables of std::pair not member methods. Drop the parentheses:

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

다른 팁

You don't need () to access the .first and .second of std::pair members. They're variable members, not methods.

Remove them:

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

first & second are not member functions. You cannot use them like a function. Remove the parentheses. Also instead of making logd an array, you can use a vector something like this

std::vector< std::list< std::pair< std::string, std::string > > > logd;

Also it will prevent the unnecessary allocation of memory.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top