0

The code:

HEADER

class Parser{

private:
    unsigned int cant_documentos;
    unsigned int cant_terminos;
    std::map<std::string,short> dicc_stopwords;
    std::map<std::string,unsigned int> hash_frecuencias_globales;
    std::map<std::string,std::map<std::string,unsigned int> > hash_frecuencias_locales;
    std::map<std::string,std::string> hash_apariciones_unicas;
public:
    Parser();
    ~Parser();


public:
    void setFrecuenciasGlobales(std::map<std::string,std::map<std::string,unsigned int> > frecuencias);
};

END OF HEADER

.CPP

void Parser::setFrecuenciasGlobales(map<string,map<string,unsigned int> > frecuencias){
hash_frecuencias_globales = frecuencias;
cant_terminos = frecuencias.size();
}

END OF CPP

COMPILER OUTPUT:

parser/parser.cpp:102:30: error: no match for ‘operator=’ in ‘((Parser*)this)->Parser::hash_frecuencias_globales = frecuencias’
parser/parser.cpp:102:30: note: candidate is:
/usr/include/c++/4.6/bits/stl_map.h:253:7: note: std::map<_Key, _Tp, _Compare, _Alloc>& std::map<_Key, _Tp, _Compare, _Alloc>::operator=(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::basic_string<char>, _Tp = unsigned int, _Compare = std::less<std::basic_string<char> >, _Alloc = std::allocator<std::pair<const std::basic_string<char>, unsigned int> >, std::map<_Key, _Tp, _Compare, _Alloc> = std::map<std::basic_string<char>, unsigned int>]
/usr/include/c++/4.6/bits/stl_map.h:253:7: note:   no known conversion for argument 1 from ‘std::map<std::basic_string<char>, std::map<std::basic_string<char>, unsigned int> >’ to ‘const std::map<std::basic_string<char>, unsigned int>&’

Where is the problem?

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
user3013172
  • 1,637
  • 3
  • 15
  • 26

3 Answers3

6

hash_frecuencias_globales is a std::map<std::string,unsigned int>, and you are trying to assign a std::map<std::string,std::map<std::string,unsigned int> > to it:

void Parser::setFrecuenciasGlobales(map<string,map<string,unsigned int> > frecuencias){
  hash_frecuencias_globales = frecuencias; // oops!

As to passing frequencias by value, this would only make sense if you were to move from it, or call std::map::swap. For a simple assignment, it would be better to pass by const reference and avoid unnecessary copies.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I would assume he grabbed the hash_frecuencias_locales type by mistake. A common enough error, fixed as you describe. (But, please used const&, passing a map by value is evil.) – RichardPlunkett Dec 02 '13 at 14:29
2

freceuncias is a map from string to a map from string to unsigned int:

std::map<std::string, std::map<std::string, unsigned int> > frecuencias

You're trying to assign this into hash_frecuencias_globales, which is a map from string to unsigned int:

std::map<std::string,unsigned int> hash_frecuencias_globales;

That's not possible, hence the error. Perhaps you only wanted to assign only the part of frequencias corresponding to a particular key?


As a side note (and also pointed out by @RichardPlunkett), you should pass big objects such as maps by const-reference instead of by value if you intend to only inspect them or copy parts of them - this will save a lot of unnecessary copying.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • not quite his error, in terms of the patch, you need to read the function name ot know whats actually gone wrong. – RichardPlunkett Dec 02 '13 at 14:27
  • 1
    @RichardPlunkett Er? The error is simply that assigning from `frequencias` into `hash_frequencias_globales` has a type mismatch. – Angew is no longer proud of SO Dec 02 '13 at 14:32
  • yes, that's the error in a compiler sense, but in a human sense, the error was that he used the type from the wrong target inthe argument list. The actual patch was to correct the type to match what he was assigning it too, not "Perhaps you only wanted to assign only the part of frequencias corresponding to a particular key?" Hence, my suggesting your patch idea wasn't ideal. – RichardPlunkett Dec 02 '13 at 14:46
1

Note the type of frecuencias, it's not the same type of map as hash_frecuencias_globales.

std::map<std::string,unsigned int> hash_frecuencias_globales;
map<string,map<string,unsigned int> > frecuencias
gmanolache
  • 497
  • 4
  • 12