0

Got lots of error when compiling the following: g++ -std=c++11 delme.cc (the source is taken from another SO question)

//delme.cc
#include<unordered_map>
#include <iostream>
#include <string.h>
#include <functional>
//using namespace std;

struct my_equal_to : public std::binary_function<char*, char*, bool>  {  
    bool operator()(char* __x, char* __y)  
    { return strcmp( __x, __y ) == 0; }  
};


struct Hash_Func{
    //BKDR hash algorithm
    int operator()(char * str)const
    {
        int seed = 131;//31  131 1313 13131131313 etc//
        int hash = 0;
        while(*str)
        {
            hash = (hash * seed) + (*str);
            str ++;
        }

        return hash & (0x7FFFFFFF);
    }
};

//typedef unordered_map<char*, unsigned int, Hash_Func,  my_equal_to> my_unordered_map;

int main(){
    //my_unordered_map location_map;
    std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to>  location_map;
    //my_equal_to location_map;
    char *p;
    char a[10] = "ab";  p = a;
    location_map.insert(std::pair<char*, unsigned int>(p, 10));
    char b[10] = "abc"; p = b;
    location_map.insert(std::pair<char*, unsigned int>(p, 20));

    char c[10] = "abc"; p = c;
    location_map.insert(std::pair<char*, unsigned int>(p, 20));

    printf("map size: %d\n", location_map.size());
    std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to>::iterator it;
    if ((it = location_map.find("abc")) != location_map.end())
    {
        printf("found!\n");
    }

    return 0;
}

There error message is too long, here is part of it

delme.cc: In function ‘int main()’:
delme.cc:44:49: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘std::_Hashtable<char*, std::pair<char* const, unsigned int>, std::allocator<std::pair<char* const, unsigned int> >, std::_Select1st<std::pair<char* const, unsigned int> >, my_equal_to, Hash_Func, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::size_type {aka long unsigned int}’ [-Wformat]
delme.cc:46:38: warning: deprecated conversion from string constant to ‘std::_Hashtable<char*, std::pair<char* const, unsigned int>, std::allocator<std::pair<char* const, unsigned int> >, std::_Select1st<std::pair<char* const, unsigned int> >, my_equal_to, Hash_Func, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::key_type {aka char*}’ [-Wwrite-strings]
In file included from /usr/include/c++/4.7/bits/hashtable.h:36:0,
                 from /usr/include/c++/4.7/unordered_map:46,
                 from delme.cc:1:
/usr/include/c++/4.7/bits/hashtable_policy.h: In instantiation of ‘static bool std::__detail::_Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>::_S_equals(const _Equal&, const _ExtractKey&, const _Key&, _HashCodeType, std::__detail::_Hash_node<_Value, true>*) [with _Key = char*; _Value = std::pair<char* const, unsigned int>; _ExtractKey = std::_Select1st<std::pair<char* const, unsigned int> >; _Equal = my_equal_to; _HashCodeType = long unsigned int]’:
/usr/include/c++/4.7/bits/hashtable_policy.h:887:23:   required from ‘bool std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_M_equals(const _Key&, std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_Hash_code_type, std::__detail::_Hash_node<_Value, __cache_hash_code>*) const [with _Key = char*; _Value = std::pair<char* const, unsigned int>; _ExtractKey = std::_Select1st<std::pair<char* const, unsigned int> >; _Equal = my_equal_to; _H1 = Hash_Func; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; bool __cache_hash_code = true; std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, __cache_hash_code>::_Hash_code_type = long unsigned int]’
...

Any ideas?

g++ (v.4.7.3) runs on Ubuntu 12.04.

Update 1

Based the suggestions to this questions, here is the working one. The command that's used to compile is: g++ -std=c++11 -fpermissive delme.cc.

#include<unordered_map>
#include <iostream>
#include <string.h>
#include <functional>
//using namespace std;

struct my_equal_to : public std::binary_function<char*, char*, bool>  {  
    bool operator()(char* __x, char* __y)  
    { return strcmp( __x, __y ) == 0; }  
};


struct Hash_Func{
    //BKDR hash algorithm
    int operator()(char * str)const
    {
        int seed = 131;//31  131 1313 13131131313 etc//
        int hash = 0;
        while(*str)
        {
            hash = (hash * seed) + (*str);
            str ++;
        }

        return hash & (0x7FFFFFFF);
    }
};

//typedef unordered_map<char*, unsigned int, Hash_Func,  my_equal_to> my_unordered_map;
char big[0x10001];
int main(int argc, char *argv[]){
    //my_unordered_map location_map;
    std::unordered_map<char*, unsigned int, Hash_Func, my_equal_to>  location_map;
    //my_equal_to location_map;
    char cmd[100];
    char *p;
    int i;
    for (i=0; i<0x10000; i++) big[i] = 'a';
    char a[10] = "ab";  p = a;
    for (i=0; i<0x100; i++) {
        big[i] = 'b';
        location_map[big] = i;
    }
    std::cout << "map size: " << location_map.size() << "\n";
    //printf("map size: %d\n", location_map.size());
    printf("result: %d\n", location_map[argv[1]]);
    gets(cmd);
    return 0;
}
6
  • So basically the errors say "don't mismatch types with printf" and "don't use a pointer to non-constant data to point to constant data" Commented Apr 16, 2015 at 2:46
  • 1
    Do you get an error message or do you mean the warnings? Commented Apr 16, 2015 at 2:46
  • 1
    If it's just the warnings I'd recommend removing the usage of printf and use std::cout. Commented Apr 16, 2015 at 2:47
  • @JamesAdkison you are right, those are warnings. They are too many and too long and so I took for granted they are errors. Turned out adding "-fpermissive" as a command line option will suppress most of them. Commented Apr 16, 2015 at 3:44
  • While you may be able suppress warnings, in general you shouldn't (there are cases where it is okay to do so but it should be done with care). In this case the printf would actually not work correctly if the value returned by size() overflowed the maximum representable value supported by int. Commented Apr 16, 2015 at 3:49

1 Answer 1

2

delme.cc:44:49: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ...

This isn't an error it's a warning and it's stating that %d is for type int however unordered_map::size doesn't return an int.

In my opinion it would be better to use a type safe operation for this printing. For example.

// This works correctly regardless of the type returned by size(), assuming
// the type is supported by operator<<
std::cout << "map size: " << location_map.size() << "\n";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.