undefined reference to std::hash<string> - c++

I'm trying to write a simple factory function for std::unordered_map. The function takes in an iterable which has a begin and end method and whose value_type is a std::pair. The following is the code that I come up with.
#include <string>
#include <unordered_map>
#include <cassert>
#include <algorithm>
template <class Iterable>
std::unordered_map<typename Iterable::value_type::first_type,
typename Iterable::value_type::second_type>
make_unordered_map(Iterable const &iter)
{
return std::unordered_map<typename Iterable::value_type::first_type,
typename Iterable::value_type::second_type>(
iter.begin(), iter.end());
}
int main()
{
std::unordered_map<std::string, int> map =
{{"a", 0}, {"b", 1}, {"c", 2}};
auto result = make_unordered_map(map);
assert(std::equal(result.begin(), result.end(), map.begin()));
return 0;
}
However, I get a long list of linker error, and it basically asks for the std::hash class specialized for std::string.
undefined reference to `std::hash<std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const>::operator()(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >) const'
I'm using GCC 4.6.1, with the -std=c++0x option. I'm pretty sure std::hash<std::string> is defined in basic_string.h, which is included in <string>.
Any idea how this happens?

You're getting your type deduction mixed up. It's important to remove qualifiers from the deduced types, so declare your template like this:
#include <functional>
template <class Iterable>
std::unordered_map<typename std::decay<typename Iterable::value_type::first_type>::type,
typename Iterable::value_type::second_type>
make_unordered_map(Iterable const &iter)
{
return std::unordered_map<
typename std::decay<typename Iterable::value_type::first_type>::type,
typename Iterable::value_type::second_type>(iter.begin(), iter.end());
}
Without this, you end up with const std::string as the key type, for which there is no specialization of std::hash.
Check out how real-world library code is written (e.g. the GCC standard library implementation) to see how to handle template types judiciously.
(By the way, std::equal is probably not the best tool for unordered maps.)

Related

Priority Queue With a Custom Comparator No Matching Constructor

I am trying to create a priority queue with a custom comparator but the following code gives me a compile error:
auto comparator = [](std::pair<std::vector<int>, File&> const &a, std::pair<std::vector<int>, File&> const &b) {
return a.first.front() > b.first.front();
};
std::priority_queue<std::pair<std::vector<uint64_t>, File&>,
std::vector<std::pair<std::vector<uint64_t>, File&>>,
decltype(comparator)> pq;
This is the error I am getting:
In template: no matching constructor for initialization of 'std::priority_queue<std::pair<std::vector, moderndbs::File &>, std::vector<std::pair<std::vector, moderndbs::File &>>, (lambda at
There's a mismatch in your question, between uint64_t and int. Assuming that this is squared away:
You need at least C++20 in order to compile the shown code. Prior to C++20, lambdas are not default-constructible, and you attempting to default-construct your std::priority_queue.
You need to explicitly pass the comparator to the constructor. Tested with gcc 10, with -std=c++17:
#include <queue>
#include <functional>
#include <vector>
#include <cstdint>
class File{};
void foo()
{
auto comparator = [](std::pair<std::vector<int>,
File &> const &a,
std::pair<std::vector<int>,
File &> const &b) {
return a.first.front() > b.first.front();
};
std::priority_queue<std::pair<std::vector<int>,
File &>,
std::vector<std::pair<std::vector<int>,
File &>
>,
decltype(comparator)> pq{comparator};
}
This fails with with a default-constructor pq. gcc 10 compiles the default-constructed pq with -std=c++20.
P.S.: consider replacing the File & with std::reference_wrapper, unless you really intend to do what the File & in std::pair will really do.

error for hash function of pair of ints

I have the following class with an unordered_map member, and a hash function defined for pair<int,int>
class abc
{public :
unordered_map < pair<int,int> , int > rules ;
unsigned nodes;
unsigned packet ;
};
namespace std {
template <>
class hash < std::pair< int,int> >{
public :
size_t operator()(const pair< int, int> &x ) const
{
size_t h = std::hash<int>()(x.first) ^ std::hash<int>()(x.second);
return h ;
}
};
}
But I am getting the following errors :
error: invalid use of incomplete type ‘struct std::hash<std::pair<int, int> >
error: declaration of ‘struct std::hash<std::pair<int, int> >
error: type ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<int, int> >, true>’ is not a direct base of ‘std::__detail::_Hash_code_base<std::pair<int, int>, std::pair<const std::pair<int, int>, int>, std::__detail::_Select1st, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>’
Unfortunately, this program has undefined behavior. C++11 §17.6.4.2.1:
A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
hash<pair<int,int>> depends on primitive and standard library types only. This is easily worked around by defining your hash class outside of namespace std, and using that hash explicitly in your map declaration:
struct pairhash {
public:
template <typename T, typename U>
std::size_t operator()(const std::pair<T, U> &x) const
{
return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
}
};
class abc {
std::unordered_map<std::pair<int,int>, int, pairhash> rules;
};
EDIT: I've used xor to combine the hashes of the pair members here because I'm lazy, but for serious use xor is a fairly crappy hash combining function.
I prefer to rely on the standard implementation of std::hash<uintmax_t> to mix hashes of components of an std::pair:
#include <functional>
#include <utility>
struct hash_pair final {
template<class TFirst, class TSecond>
size_t operator()(const std::pair<TFirst, TSecond>& p) const noexcept {
uintmax_t hash = std::hash<TFirst>{}(p.first);
hash <<= sizeof(uintmax_t) * 4;
hash ^= std::hash<TSecond>{}(p.second);
return std::hash<uintmax_t>{}(hash);
}
};

c++ no matching function for call to: passing iterators?

I've encountered a strange problem in my C++ Code.
I've defined a TemplateClass called
StateTemplate<T>
Furthermore I defined
typedef StateTemplate<double> StateDouble.
Then I defined a friend function
template<class S>
friend S partialEntangledScalarProduct(
const typename std::vector<StateTemplate<T> >::const_iterator s1,
const typename std::vector<StateTemplate<T> >::const_iterator s2,
const typename std::vector<StateTemplate<T> >::const_iterator sSub1,
const typename std::vector<StateTemplate<T> >::const_iterator sSub2,
vector<unsigned int> const &pos,
vector<unsigned int> const &posNot);
If I now make
std::vector<StateDouble>::const_iterator s1;
std::vector<StateDouble>::const_iterator s2;
std::vector<StateDouble>::const_iterator sSub1;
std::vector<StateDouble>::const_iterator sSub2;
vector<unsigned int> vui;
Doub test=partialEntangledScalarProduct(s1,s2,sSub1,sSub2,vui,vui);
I get the following Error:
no matching function for call to
'partialEntangledScalarProduct(
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
const std::vector<unsigned int, std::allocator<unsigned int> >&,
std::vector<unsigned int, std::allocator<unsigned int> >&)'
I've tried for hours to find the problem, but it seems that I overlook something.
Maybe someone could help me?
best regards
Dominik
p.s.: if you need more information about my code, just let me know. At the moment I just wanted to give you the important parts.
Here is a small example with the same error.
#include <iostream>
#include <vector>
template<class T>
class StateTemplate{
template<class S>
friend S testFunction(
const typename std::vector<StateTemplate<S> >::const_iterator s1);
public:
StateTemplate();
};
template<class T>
StateTemplate<T>::StateTemplate(){}
template<class T>
T testFunction(
const typename std::vector<StateTemplate<T> >::const_iterator s1)
{
return T(1);
}
int main(){
std::vector<double>::const_iterator s1;
double s=testFunction<double>(s1);
return 0;
}
If you presented the declaration of partialEntangledScalarProduct correctly, the template parameter S isn't used anywhere in the function parameter list. Therefore, the compiler would never deduce it and you have to provide it explicitly.
partialEntangledScalarProduct<SomeClass>(s1,s2,sSub1,sSub2,vui,vui);
SomeClass is just an example, the exact name is unclear unless you provide more details.
(I don't have g++ on this machine, so this is conjecture.)
I think the issue is that the compiler can't deduce S and you're getting an unhelpful error message. Change your usage to the following:
double test = partialEntangledScalarProduct<double>(s1,s2,sSub1,sSub2,vui,vui);
As a side note, make sure your examples compile for you before you post them. I'm assuming Doub was supposed to be double?

Why does this std::sort predicate fail when the class is inside main()?

This is a much simplified repro which illustrates how class Predicate delcared outside main() works but when the exact code appears inline as class InlinePredicate the compiler can't match std::sort. The strange thing is that you can pass anything as the third argument to std::sort (say, integer 7) and you'll just get a compile error when it does not support the operator () that sort expects. But when I pass pred2 below it doesn't match at all:
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Predicate {
public:
bool operator () (const pair<string,int>& a, const pair<string,int>& b)
{
return a.second < b.second;
}
};
int
main()
{
vector<pair<string, int> > a;
Predicate pred;
sort(a.begin(), a.end(), pred);
class InlinePredicate {
public:
bool operator () (const pair<string,int>& a, const pair<string,int>& b)
{
return a.second < b.second;
}
} pred2;
sort(a.begin(), a.end(), pred2);
return 0;
}
repro.cc: In function ‘int main()’:
repro.cc:30: error: no matching function for call to ‘sort(__gnu_cxx::__normal_iterator, std::allocator >, int>*, std::vector, std::allocator >, int>, std::allocator, std::allocator >, int> > > >, __gnu_cxx::__normal_iterator, std::allocator >, int>*, std::vector, std::allocator >, int>, std::allocator, std::allocator >, int> > > >, main()::InlinePredicate&)’
In C++03, local classes have no linkage and consequently cannot be used as template arguments (§14.3.1/2).
In C++0x, this limitation has been removed and your code would compile as-is.
In versions of C++ prior to C++0x, classes declared inside functions cannot appear in template parameters. Your invocation of sort implicitly instantiates it with a template parameter set to InlinePredicate, which is illegal.
You may want to consider using either C++0x (with GCC, pass --std=c++0x; in C++0x this code will work as-is, or you can use anonymous functions instead), or boost::lambda. With boost::lambda, it would look like this:
using namespace boost::lambda;
sort(a.begin(), a.end(), _1 < _2);

find() problems

I've got an error while using find() function. Here is the code:
#include <iostream>
#include <map>
#define N 100000
using namespace std;
int main (int argc, char * const argv[]) {
map<int,int> m;
for (int i=0; i<N; i++) m[i]=i;
find(m.begin(), m.end(), 5);
return 0;
}
I'm getting an compiller error:
error: no match for 'operator==' in '__first. __gnu_debug::_Safe_iterator<_Iterator, _Sequence>::operator* [with _Iterator = std::_Rb_tree_iterator<std::pair<const int, int> >, _Sequence = __gnu_debug_def::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]() == __val'
Including 'algorithm' nothing changes. Compiling in VS2008 shows similar error.
I know about m.find(), but I realy need to use find() too.
Thanks a lot for your assistance!
P.S. Actualy, the task is to compare speed of m.find(5) and find(m.begin(), m.end(), 5), so I need to make both of them work properly.
begin() and end() on all STL containers provide access to elements of those collections. Type of those elements is known as value_type of the container. For std::map<Key, Value>, its value_type is std::pair<Key, Value>. Therefore, your find function is trying to find a pair<int, int> which is equal to 5. Since there's no operator== defined to compare pair<int, int> and int, you get the error.
The correct way to do this (so long as you want to avoid member find()) is to use std::find_if:
template <class First>
struct first_equal
{
const First value;
first_equal(const First& value)
: value(value)
{
}
template <class Second>
bool operator() (const std::pair<First, Second>& pair) const
{
return pair.first == value;
}
};
...
find_if(m.begin(), m.end(), first_equal<int>(5));
You could also overload operator== for pair and int to do what you want, but it's a very hackish way (because it will affect all your code, and because such a comparison has no meaning in general).
find() requires a parameter that can be compared to *iterator. For your map, this will be pair<int,int>. You'll need to create a dummy pair, plus a comparison functor to compare the pairs.
Just use m.find(5)