c++ - 错误 : expected a type, 得到分配器

标签 c++ vector dictionary allocator

我试图在 RHEL 上编译一个非常旧的 cpp 代码,但遇到了以下错误。我不确定为什么编译器会用一个值代替分配器?这是什么意思,有人可以指出问题所在以及解决方案吗? 欣赏它。

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

#include<memory>     
#include<string>     

using std::map;      
using std::string;   
using std::vector;   

class Clue {
 public:
  Clue() {std::cout "Clue" << endl;}
};

class Field {
 public:
  Field() {std::cout "Field" << endl;}
};

class S
{
 public:
  typedef map<string,Field,std::less<string>, std::allocator>       tFieldsList;
      typedef map<string ,Field, std::less<string>, std::allocator >::const_iterator
                                                                 cFieldsIter;
      typedef map<string ,Field, std::less<string>, std::allocator >::iterator FieldsIter;
   typedef vector<Clue,std::allocator>  tVectorClue;

};

int main () {
 S s;
 return 0;
}

这是我遇到的错误:

allocatortypemismathch.C: In constructor âClue::Clue()â:
allocatortypemismathch.C:14: error: expected â;â before string constant
allocatortypemismathch.C: In constructor âField::Field()â:
allocatortypemismathch.C:19: error: expected â;â before string constant
allocatortypemismathch.C: At global scope:
allocatortypemismathch.C:25: error: type/value mismatch at argument 4 in template parameter list for âtemplate<class _Key, class _Tp, class _Compare, class _Alloc> class std::mapâ
allocatortypemismathch.C:25: error:   expected a type, got âallocatorâ
allocatortypemismathch.C:26: error: type/value mismatch at argument 4 in template parameter list for âtemplate<class _Key, class _Tp, class _Compare, class _Alloc> class std::mapâ
allocatortypemismathch.C:26: error:   expected a type, got âallocatorâ
allocatortypemismathch.C:27: error: expected â;â before âcFieldsIterâ
allocatortypemismathch.C:28: error: type/value mismatch at argument 4 in template parameter list for âtemplate<class _Key, class _Tp, class _Compare, class _Alloc> class std::mapâ
allocatortypemismathch.C:28: error:   expected a type, got âallocatorâ
allocatortypemismathch.C:28: error: expected â;â before âFieldsIterâ
allocatortypemismathch.C:29: error: type/value mismatch at argument 2 in template parameter list for âtemplate<class _Tp, class _Alloc> class std::vectorâ
allocatortypemismathch.C:29: error:   expected a type, got âallocatorâ

最佳答案

std::allocator是一个模板。您需要添加如下 std::allocator<std::pair<const string, Field>> :

class Clue {
 public:
  Clue() {std::cout << "Clue" << std::endl;}
};

class Field {
 public:
  Field() {std::cout << "Field" << std::endl;}
};

class S
{
 public:
   typedef map<string,
               Field,std::less<string>,
               std::allocator<std::pair<const string, Field>>
               > tFieldsList;
   typedef map<string,
               Field,
               std::less<string>,
               std::allocator<std::pair<const string, Field>>
               >::const_iterator cFieldsIter;
   typedef map<string,
               Field,
               std::less<string>,
               std::allocator<std::pair<const string, Field>>
               >::iterator FieldsIter;
   typedef vector<Clue,std::allocator<Clue>> tVectorClue;

};

此外,您还有一些拼写错误,例如 std::cout "Clue" << endl;在你的代码中,应该是 std::cout << "Clue" << std::endl;

但是,您做的工作太多了!您可以像这样更改代码,因为第三个和第四个模板参数具有 std::map 的默认模板参数。 std::vector 的第二个参数,因此您无需提供:

typedef map<string,Field>                  tFieldsList;
typedef map<string ,Field>::const_iterator cFieldsIter;
typedef map<string ,Field>::iterator       FieldsIter;
typedef vector<Clue>                       tVectorClue;

关于c++ - 错误 : expected a type, 得到分配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18715450/

相关文章:

c++ - std::chrono::milliseconds 的 vector

performance - 附加到向量的效率

c++ - 错误 C2440 : 'return' : cannot convert from 'int [2]' to 'int (&&)[2]'

c++ - Getter 函数返回错误值?

c++ - 引用指针和引用重载

c++ - C++程序输出问题

dictionary - 有限 map 示例

scala - 为什么 Scala 中没有可变的 TreeMap?

python - 只保留一个条目而不是 append 到字典

c++ - 程序使用 boost 库编译正常,但运行时出错