I don't know anything about C++, I am a C programmer, but I need to compile a tool that is written in C++, and I am stopped with this error:
map.cpp
/usr/lib/gcc-v10.2.0/include/g++-v10/backward/hashtable.h:604:23:
error: no match for call to ‘(const hasher {aka const __gnu_cxx::hash<long long unsigned int>}) (const key_type&)’
604 | { return _M_hash(__key) % __n; }
| ~~~~~~~^~~~~~~
which can be reproduced by the following testing-example:
#include <fstream>
#include <iostream>
#include "map.h"
#include <ext/hash_map>
#include <string>
#include <sstream>
using namespace std;
using __gnu_cxx::hash_multimap;
int nav() {
cout <<"Select from the following options : " << endl <<endl;
cout <<"Search Tweets based on Keyword (Type 1) " <<endl;
cout <<"End Program (Type 2)"<<endl<<endl;
int key =0;
cin >> key;
return key;
}
int main() {
int option = nav();
if (option == 1) {
ifstream readFile("project4.csv");
string tempPop, tempID, tempKey, tempUser, tempDesc;
string tempRead;
hash_multimap<string, Map2 *>map1;
while (readFile != NULL){
// sends to a temp variable
readFile >> tempRead;
for (int i =0; i<400; i++){
//create new object each time
Map2 *mapNode = new Map2(tempPop,tempID,tempKey,tempUser,tempDesc);
//insert each time new object is made
map1.insert(pair<string, Map2 *> (tempKey, mapNode));
} //end for
} //end while
//Navigation through multimap
//first pointer is for first one and second to last hash table value
pair<hash_multimap<string, Map2 *> :: const_iterator,
hash_multimap<string, Map2 *> :: const_iterator> p;
string searchKey = "";
cout << "Please enter the keyword value exactly so we can search the"<<
"available tweets: " <<endl;
cin >> searchKey;
p = map1.equal_range(searchKey);
}
else
return 0;
}
map.h
#ifndef MAP2_H
#define MAP2_H
#include <iostream>
#include <string>
using namespace std;
class Map2 {
public:
Map2(string data1, string data2, string data3, string data4, string data5);
string pop, keyword, user, desc, id;
string get_pop() {return pop;}
string get_key() {return keyword;}
string get_user() {return user;}
string get_desc() {return desc;}
string get_id() {return id;}
void call_Values(int i);
};
Map2:: Map2(string data1, string data2, string data3, string data4, string data5) {
pop = data1;
keyword = data2;
user = data3;
desc = data4;
id = data5;
}
void Map2:: call_Values(int i) {
get_pop();
get_key();
get_user();
get_desc();
get_id();
}
#endif
Anyone has an idea? Workaround? or any doc-suggestion to look at?
This is the line where g++ traps error
/usr/lib/gcc-v10.2.0/include/g++-v10/backward/hashtable.h:604:23:
size_type
_M_bkt_num_key(const key_type& __key, std::size_t __n) const
{ return _M_hash(__key) % __n; }
When asking stuff like this always post the entire set of error messages. When I have tried to compile your code, there was a lot more going on, this is important to see.
E.g. this doesn't bother you?
hash.cpp: In function ‘int main()’:
hash.cpp:34:25: error: no match for ‘operator!=’ (operand types are ‘std::ifstream’ {aka ‘std::basic_ifstream<char>’} and ‘long int’)
while (readFile != NULL)
^
hash.cpp:34:25: note: candidate: ‘operator!=(int, long int)’ <built-in>
That's literally the first error I get when trying to compile your code. You can't compare ifstream with a pointer (and at that note - don't use NULL but nullptr in C++). Without fixing that there is no point dealing with the rest of errors because one failure in compiling the templates will propagate and cause a lot of other errors as a consequence.
* Don't use ext/hash_map - that's old, deprecated and non-standard. If you want a unordered hash table, use unordered_map instead, otherwise normal map. See e.g.:
http://www.cplusplus.com/reference/unordered_map/unordered_multimap/ (http://www.cplusplus.com/reference/unordered_map/unordered_multimap/)
* Don't name your headers the same as the system headers map.h is also a system header and this a good way to shoot yourself in the foot because yours could get included before the system one.
When asking stuff like this always post the entire set of error messages. When I have tried to compile your code, there was a lot more going on, this is important to see.
Ok, so forget the posted code above; when I try to compile the big project, the first error I encounter is:
/usr/lib/gcc/9.3.0/include/g++-v9/backward/hashtable.h:609:31: error: no match for call to ‘(const hasher {aka const __gnu_cxx::hash<long long unsigned int>}) (const key_type&)’
609 | { return _M_hash(__key) % __n; }
ac_hltrace.cpp:409:13: warning: ‘bool print_dwarf_function(Dwfl_Module*, Dwarf_Addr, LineInfo*)’ defined but not used [-Wunused-function]
409 | static bool print_dwarf_function (Dwfl_Module *mod, Dwarf_Addr addr, LineInfo* lineInfo)
| ^~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/9.3.0/include/g++-v9/backward/hashtable.h:609
607: size_type
608: _M_bkt_num_key(const key_type& __key, size_t __n) const
609: { return _M_hash(__key) % __n; }
Note I am using g++ v9.3.0 now.
The same happens with g++ v4.7
I also had weird errors cause by Bison in a dozen files, because it made files which then g++ complained about "error: invalid conversion from ‘const char*’ to ‘char*’ " and "ISO C++ forbids converting a string constant to ‘char*’ ".
This is one of the twelve lines in the Makefiles that exposes this issues in the generated "axm.tab.c" file
axm.tab.c: Parser/axm.y
bison -v --name-prefix=axm -d Parser/axm.y
I used /usr/bin/sed to manually change "const char" into "char" before Makefiles invokes C++.
$(CXX) $(CXX_FLAGS) -c axm.tab.c -o axm-parser.o
Ugly, but at least it compiles and seems working (edit: it has just passed all the tests) :D
I think this project needs a serious rewrite. There are too many parts with ugly things. Unfortunately I am not skilled with C++ and Bison (never used it), so I can't do any definite fix.
I am trying to contact the authors. Hope they will fix stuff once for all.