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?
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.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.