mercredi 25 octobre 2017

Find Iterator in a std::vector

I'm having a problem, i'm trying to create a System to read and write .ini files, but i'm stuck at how to correctly add the sections/keys. I thought i could add them as objects to a std::vector, so i wrote the following snippet (i removed code that is irrelevant):

// .h

class Ini
{
public:
    class IniSection
    {
        friend class Ini;

    private:
        IniSection(Ini *ini, std::string section);
        ~IniSection();

    private: 
        Ini *pIni;
        std::string sectionName;
    };

    vector<IniSection *>::iterator Ini::FindSection(std::string section);

    IniSection *AddSection(std::string name);

private:
    vector<IniSection *> Sections;
};

typedef Ini::IniSection IniSection;



// .cpp


IniSection::IniSection(Ini *ini, std::string section) : pIni(ini), sectionName(section)
{

}

vector<IniSection *>::iterator Ini::FindSection(std::string section)
{
    IniSection tempSection(NULL, section);

    return find(Sections.begin(), Sections.end(), tempSection); // ERROR
}

IniSection *Ini::AddSection(std::string name)
{
    vector<IniSection *>::const_iterator iter = FindSection(name);

    if (iter == Sections.end())
    {
        IniSection *newSection = new IniSection(this, name);
        Sections.push_back(newSection);
        return newSection;
    }
    else
        return *iter;
}

As you can see i marked a line with

// ERROR

that's because when i try to build it, i get this error:

Error C2679 binary '==' : no operator found which takes a right-hand operand of type 'const Ini::IniSection' (or there is no acceptable conversion)

It occurs in the algorithm library. So i guess the way i try to get the iterator is wrong, so maybe someone is able to help me out. I couldn't find something on the Internet that suited me. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire