类型为typedef map的类的编译器错误<std :: string,std :: pair <std :: string,vector <int>&gt;&gt; MapPairList; </的std :: string,性病::对<的std :: string,矢量<int>的

时间:2012-03-09 12:12:09

标签: c++ data-structures compiler-errors

这是我的代码。

类xyz。

#include <iostream>
#include <vector>
#include <map>

using namespace std;

typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList;

class xyz
{
private:
    MapPairList m1;

public:
    void insert();
    string GetType(string& filetype);
    vector<int> GetExtList(string& filetype);
};

上述课程的实施。

    #include "xyz.h"

void xyz::insert()
{
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);

    vector<int> v2;
    v2.push_back(1);
    v2.push_back(2);
    v2.push_back(3);

    vector<int> v3;
    v3.push_back(1);
    v3.push_back(2);
    v3.push_back(3);

    string c1 = "type1";
    string f1 ="filetype1";
    string c2 = "type2";
    string f2 ="filetype2";
    string c3 = "type3";
    string f3 ="filetype3";

    m1.insert(make_pair(f1,make_pair(c1,v1)));
    m1.insert(make_pair(f2,make_pair(c2,v2)));
    m1.insert(make_pair(f3,make_pair(c3,v3)));
}

string xyz::GetType(std::string &filetype)
{
    MapPairList::iterator iter = m1.find(filetype);

    if(iter != m1.end())
    {
        return (*iter).second.first;
    }
}

vector<int> xyz::GetExtList(std::string &filetype)
{
    MapPairList::iterator iter = m1.find(filetype);

    if(iter != m1.end())
        return (*iter).second.second;
}

int main()
{
    xyz *x = new xyz();

    string out("filetype1");
    string in = x->GetType(out);
    cout<<in.c_str();

    delete x;
    return 0;
}

当我尝试编译时,我得到以下错误:

1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(1372) : see declaration of 'std::operator <'
1>        c:\program files\microsoft visual studio 8\vc\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\map(72) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(26) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=std::pair<std::string,std::vector<int>>,
1>            _Pr=std::less<std::string>,
1>            _Alloc=std::allocator<std::pair<const std::string,std::pair<std::string,std::vector<int>>>>,
1>            _Mfl=false
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(68) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,std::pair<std::string,std::vector<int>>,std::less<std::string>,std::allocator<std::pair<const std::string,std::pair<std::string,std::vector<int>>>>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(94) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,std::pair<std::string,std::vector<int>>,std::less<std::string>,std::allocator<std::pair<const std::string,std::pair<std::string,std::vector<int>>>>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(112) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,std::pair<std::string,std::vector<int>>,std::less<std::string>,std::allocator<std::pair<const std::string,std::pair<std::string,std::vector<int>>>>,false>
1>        ]
1>        c:\program files\microsoft visual studio 8\vc\include\map(82) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,std::pair<std::string,std::vector<int>>,std::less<std::string>,std::allocator<std::pair<const std::string,std::pair<std::string,std::vector<int>>>>,false>
1>        ]
1>        c:\documents and settings\apoos\my documents\visual studio 2005\projects\algos\maplistpair\xyz.h(12) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=std::string,
1>            _Ty=std::pair<std::string,std::vector<int>>
1>        ]
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(1372) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(1372) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xtree(1372) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\vector(1276) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\vector(1276) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\vector(1276) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\vector(1276) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xutility(1880) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xutility(1880) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xutility(1880) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\xutility(1880) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\utility(76) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\utility(76) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\utility(76) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const std::string'
1>        c:\program files\microsoft visual studio 8\vc\include\utility(76) : see declaration of 'std::operator <'
1>c:\program files\microsoft visual studio 8\vc\include\functional(143) : error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

实际上我只想使用特定的数据结构。

如果你有更好的解决方案来替换这个数据结构,请建议。元素的映射是容器在前两个字符串之间是一对一的。并且在对中的元素之间有一对多。这就是为什么对中的最后一个元素是向量的原因。 谁能告诉我出了什么问题?

2 个答案:

答案 0 :(得分:3)

您尚未加入<string>。在#include <string>之前添加typedef...

答案 1 :(得分:2)

g ++说error: ‘>>’ should be ‘> >’ within a nested template argument list

将您的typedef更改为:

typedef map<std::string,std::pair<std::string,vector<int> > > MapPairList;