Search code examples
gccstd-pairc++98stdlist

List of pairs of strings initialization in constructor


I have two classes, the scheduler and the cshw class. The constructor of the scheduler requires std::list < std::pair<std::string, std::string> > names. The CSHW class inherits from the scheduler and in the constructor of the cshw class i do cshw::cshw():scheduler ({std::make_pair("mfp", "MFP") }){ i get a symbol not found error. I basically need to pass a list of pairs of strings to the scheduler constructor, this works in c++11 but i am facing this problem only in c++98


#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <list>
#include <utility>
#include <string>

class scheduler {
public:
               std::list < std::pair<std::string, std::string>  > names;
               scheduler(std::list < std::pair<std::string, std::string> > names);
               virtual ~scheduler();
};

class cshw : public scheduler{
public:
               std::list < std::pair<std::string, std::string>  > m_szPanelNames;
               cshw();
               virtual ~cshw();
};

scheduler::scheduler(std::list < std::pair<std::string, std::string> > pnames) {
               names = pnames;
}

scheduler::~scheduler() {
}

cshw::cshw():
                              scheduler ({std::make_pair("mfp", "MFP") }){
}

cshw::~cshw() {
               // TODO Auto-generated destructor stub
}
#endif /* SCHEDULER_H_ */

I get a constructor does not match error for the scheduler. I think this is caused by me trying to initialize a list pair as an argument. what is the way to pass a list of pairs as an argument in c++98 ?


Solution

  • You can add helper method in cshw which generates the list:

    class cshw : public scheduler{
    public:
                   std::list < std::pair<std::string, std::string>  > m_szPanelNames;
                   cshw();
                   virtual ~cshw();
    
                  // added
                  std::list< std::pair<std::string,std::string> > genList()
                  {
                      std::list< std::pair<std::string,std::string> > l;
                      l.push_back(std::make_pair("mfp","MFP"));
                      return l;
                  }
    };
    

    then pass its result as param for scheduler on initialization list:

    cshw::cshw():
       scheduler ( genList() ) 
    {;}
    

    Live demo