c++ - typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList 类型数据成员类中的编译器错误;?

标签 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

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

如果您有更好的解决方案来用其他数据结构替换此数据结构,请提出建议。元素的映射是容器在第一个两个字符串之间是一对一的。对中的元素之间一对多。这就是为什么对中的最后一个元素是 vector 的原因。 谁能告诉我哪里出了问题?

最佳答案

您没有包含 <string> .添加#include <string>之前typedef... .

关于c++ - typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList 类型数据成员类中的编译器错误;?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9633862/

相关文章:

c++ - 是否可以关闭 gcc 的自动并行化?

c++ - libsoup客户端的简单例子

python - 求矩阵内 1s 的最大平方时出错

c++ - osgDB/FileUtils在我的代码中导致编译器错误

c++ - 如何确保在修改 .h 文件时,包含该文件的 .cc 文件会在使用 Visual Studio 2008 的发布版本中自动编译?

c++ - 如何注册一个类以在 Qt 中的 QWebChannel 信号中使用它

java - 如何从数组末尾删除一个元素并将其插入到前面

scala - 在Scala中使用哪种类型存储内存中的可变数据表?

matlab - 调用该功能不起作用

c++ - 为什么这个奇怪的重复出现的模板模式示例不能编译?