Question

I am trying to compile a very old cpp code on RHEL and am running into the following error. I am not sure why does the compiler crib for a value in place of an allocator? what does it mean, can someone please point out what the issue is and also the solution? Appreciate it.

#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;
}

Here is the error I am getting:

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â
Était-ce utile?

La solution

std::allocator is a template. You need to add like the following 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;

};

Also, you have some typos like std::cout "Clue" << endl; in your code, should be std::cout << "Clue" << std::endl;

However, you are doing too much work! You can change the code like this, since the third and fourth template parameter has default template arguments for std::map and the second argument for std::vector, so you don't need to provide:

typedef map<string,Field>                  tFieldsList;
typedef map<string ,Field>::const_iterator cFieldsIter;
typedef map<string ,Field>::iterator       FieldsIter;
typedef vector<Clue>                       tVectorClue;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top