错误C2535:已定义或声明的成员函数

时间:2013-10-06 05:32:46

标签: c++ visual-c++ visual-studio-2012 c++11 stl

我正试图解决这个Facebook挑战

http://learn.hackerearth.com/question/383/staves/

我正在犯错误

error C2535: 'std::vector<_Ty> &std::map<_Kty,Staves::SubStringVector &>::operator [](const std::basic_string<_Elem,_Traits,_Alloc> &)' 
: member function already defined or declared   
c:\program files (x86)\microsoft visual studio 11.0\vc\include\map  191 1   FB-Staves

我完全不知道为什么会收到此错误,有人可以帮我识别我的代码中的问题吗?

由于 萨米尔

我的代码如下所示:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

class Staves
{
public:
    struct SubString
    {
    std::string str;
    unsigned int startPos;
    unsigned int endPos;
};

struct PositionalComparer
{
    bool operator()(const SubString* s1, const SubString* s2)
    {
        return s1->startPos < s2->startPos;
    }
} PositionalComparer;

typedef std::vector<const SubString*> SubStringVector;

typedef std::map<const std::string&, SubStringVector&> SubStringMap;

public:
Staves()
{
};

virtual ~Staves()
{
    for(unsigned int i = 0; i < m_vec.size(); ++i)
    {
        delete m_vec[i];
    }
}

public:
void AddSubString(const std::string& input, unsigned int startPos, unsigned int endPos)
{
    SubString* subStr = new SubString();
    subStr->str = input.substr(startPos, endPos - startPos);
    std::sort(subStr->str.begin(), subStr->str.end());
    subStr->startPos = startPos;
    subStr->endPos = endPos;

    m_vec.push_back(subStr);
};

bool PrintStavesIfAny()
{
    SubStringMap subStrMap;

    for(unsigned int i = 0; i < m_vec.size(); ++i)
    {
        const std::string& s1 = m_vec[i]->str;
        if(subStrMap.find(s1) != subStrMap.end())
        {
            SubStringVector& v1 = subStrMap[s1];
            v1.push_back(m_vec[i]);
        }
        else
        {
            SubStringVector v2;
            v2.push_back(m_vec[i]);
            subStrMap[s1] = v2;
        }
    }

    // Now that our map is ready, iterate and find a pair
    // of substrings that do not overlap
    SubStringMap::iterator itr = subStrMap.begin();
    for(; itr != subStrMap.end(); ++itr)
    {
        if(PrintStavesIfAny(itr->second))
        {
            return true;
        }
    }

    return false;
};

private:
bool PrintStavesIfAny(SubStringVector& vec)
{
    std::sort(vec.begin(), vec.end(), PositionalComparer);

    const SubString* s = vec[0];

    for(unsigned int i = 1; i < vec.size(); ++i)
    {
        if(vec[i]->endPos > s->endPos)
        {
            cout << s->startPos << " " << vec[i]->startPos << " " << s->str.size() << endl;
            return true;
        }
    }

    return false;
};

private:
SubStringVector m_vec;
};

void PrintStaves(const string& str)
{
unsigned int currentLen = str.size() / 2;
unsigned int currentPos = 0;

while(currentLen != 0)
{
    // find all substrings of length currentLen
    currentPos = 0;
    Staves* staves = new Staves();

    while(currentPos + currentLen < str.size())
    {
        staves->AddSubString(str, currentPos, currentPos + currentLen);
        ++currentPos;
    }

    // Done adding all substrings of currentLen, now find if a stave exists
    if(staves->PrintStavesIfAny())
    {
        return;
    }

    // make a fresh beginning for a shorter stave
    delete staves;
    --currentLen;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
    string str = "131251141231";
    //getline(cin, str);
    PrintStaves(str);
    return 0;
}

1 个答案:

答案 0 :(得分:8)

typedef std::map<const std::string&, SubStringVector&> SubStringMap;

STL不允许重置引用,不能在STL容器中存储引用。您可以按值或指针存储元素。

typedef std::map<const std::string, SubStringVector> SubStringMap;