簡體   English   中英

類型為typedef映射的數據成員的類中的編譯器錯誤 <std::string,std::pair<std::string,vector<int> &gt;&gt; MapPairList ;?

[英]compiler error in class with datamember of type typedef map<std::string,std::pair<std::string,vector<int>>> MapPairList;?

這是我的代碼。

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

實際上,我正在嘗試僅使用特定的數據結構。

如果您有更好的解決方案,可以用其他數據結構代替此數據結構,請提出建議。 元素的映射是容器在第一和第二字符串之間是一對一的。 一對中的元素之間一對多,這就是為什么一對中的最后一個元素是向量。 誰能告訴我出了什么問題?

您尚未包括<string> typedef...之前添加#include <string>

g ++表示error: '>>' should be '> >' within a nested template argument list

將您的typedef更改為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM