没有构造函数可以采用源类型

no constructor could take the source type

本文关键字:类型 构造函数      更新时间:2023-10-16

我一直收到错误,没有构造函数可以采用源类型,或者构造函数重载解析。

在代码的开头,我声明了一个无序映射。

unordered_map<char * , a_dictionary * > Mymap;

    unsigned char hash[20];
    char hex_str[41];
    string answer, line;
    int yes=0;
    cout<<"Press 1 if you would like to use the default file(d8.txt) or press 2 if you want your own file"<<endl;
    getline(cin,answer);
    stringstream(answer)>> yes;
    if(yes == 1 )
    {
        ifstream myfile("d8.txt");
        if (myfile.is_open())
        {
            while ( myfile.good() )
            {
                getline (myfile,line);
                //cout<<line<<endl;
                a_dictionary * dic = new dictionary();
                dic->word = line;
                const char * c= line.c_str();
                sha1::calc(c,line.length(), hash);
                sha1::toHexString(hash,hex_str);
                Mymap.insert(hex_str, dic); // 

这里的行"mymap.insert"一直给我错误C2664:'sstd::_List_iterator&lt_Mylist>std::_Hash&lt_特征>::插入(std::_List_const_iterator<_Mylist>,_Valty(,即使我传递了正确的值,对吗?

这是调用HeXString 的函数

void toHexString(const unsigned char* hash, char* hexstring)
{
    const char hexDigits[] = { "0123456789abcdef" };
    for (int hashByte = 20; --hashByte >= 0;)
    {
        hexstring[hashByte << 1] = hexDigits[(hash[hashByte] >> 4) & 0xf];
        hexstring[(hashByte << 1) + 1] = hexDigits[hash[hashByte] & 0xf];
    }
    hexstring[40] = 0;
}

您需要将其作为一对插入。

Mymap.insert(std::make_pair(hex_str, dic));

或者使用C++11初始化程序列表

Mymap.insert({hex_str, dic});

点击此处查看示例

或者,您可以使用operator[],生成更干净的代码

Mymap[hex_str] = dic;

http://cplusplus.com/reference/unordered_map/unordered_map/insert/检查插入声明。你想要std::对。