简体   繁体   English

无法让二传手开始工作

[英]Can't get setter to work

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 .CPP

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

END OF CPP 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: hash_frecuencias_globales是一个std::map<std::string,unsigned int> ,并且您试图为其分配一个std::map<std::string,std::map<std::string,unsigned int> >

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 . 至于按值传递frequencias ,这仅在您要离开它或调用std::map::swap时才有意义。 For a simple assignment, it would be better to pass by const reference and avoid unnecessary copies. 对于简单的分配,最好通过const引用传递,并避免不必要的副本。

freceuncias is a map from string to a map from string to unsigned int : freceuncias是从string到从stringunsigned 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 : 您正在尝试将其分配给hash_frecuencias_globales ,这是从stringunsigned 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? 也许您只想分配与特定密钥相对应的frequencias部分?


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. 作为一个旁注(也由@RichardPlunkett指出),如果您仅打算检查或复制它们的一部分,则应通过const引用而不是通过值传递大对象(例如,地图)-这将节省很多不必要的东西复制。

Note the type of frecuencias , it's not the same type of map as hash_frecuencias_globales . 请注意frecuencias的类型,它与hash_frecuencias_globales的地图类型hash_frecuencias_globales

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM