defining a hash set with a compare function - c++

I am trying to do a very simple task of defining a new type of hash set which has a compare function as well.
using namespace std;
#include <iostream>
#include <ext/hash_set>
#include <hash_set>
#include <functional>
#include <hash_compare>
typedef __gnu_cxx::hash_set<int, hash_compare<int, less<int> > > hashcomp;
int main(int argc, char * const argv[]) {
}
Error: hash_compare is not defined (line 7)
Error: expected unqualified-id before ">" token (line 7)
Error: template argument 2 is invalid. (line 7)

Seeing the error, I guess you haven't included <functional> as you're using std::less<int> in your code. I'm assuming that less<int> in your code is actually std::less<int>.
EDIT:
The error message clearly says
Error: hash_compare is not defined.
What else do you want? Include the header file which defines hash_compare.

Your requirements are contradictory. A hash table has a random order, by definition.

Related

BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error

I am trying to create a lazy function from a template function following the Boost::phoenix documentation. The code looks like this
#include <iostream>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>
using namespace boost;
using namespace boost::phoenix;
namespace demo
{
bool func(double a,double b)
{
return bool(a > b);
}
}
BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)
int main(int argc,char **argv)
{
namespace pl = boost::phoenix::placeholders;
auto comperator = func(pl::arg1,pl::arg2);
std::cout<<comperator(1.2,12.4)<<std::endl;
std::cout<<comperator(0.5,0.1)<<std::endl;
}
This is virtually one of the examples from the BOOST documentation. Storing this file as mk_lazy1.cpp and try to compile gives
$ g++ -omk_lazy1 mk_lazy1.cpp
mk_lazy1.cpp:26:1: error: template argument 1 is invalid
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope
I use gcc-4.7 on a Debian testing system. An honestly I am a bit lost as I have absolutely no idea what is wrong here (as I said, this is virtually a word by word copy of one of the examples provided by the Boost documentation).
Does anyone have a good idea?
Remove using namespaces and all will work fine.
Or write using namespaces AFTER adapt macro and all will work fine too.
Or put macro into unnamed namespace.

User defined arguments in Boost Phoenix

The Boost Phoenix documentation here indicates that I can create my own (lambda) arguments instead of _1/arg1, _2,arg2 etc. So, starting with code like this:
#include <iostream>
#include <boost/phoenix.hpp>
int main(int argc, char *argv[]) {
std::cout << (boost::phoenix::arg_names::_1)(17) << std::endl;
return 0;
}
...which outputs 17, I'm aiming for a first step of using myarg1. The documentation recommends that I first #include <boost/spirit/home/phoenix/core/argument.hpp>. Doing this (G++ 4.7.2), however, results in a series of compilation errors starting with: error: redefinition of ‘struct boost::phoenix::detail::error_expecting_arguments’.
Without the arguments.hpp file included, I notice that I can declare variables of type boost::phoenix::argument<0>. When I try to declare a variable of the crucial type, boost::phoenix::actor< boost::phoenix::argument<0> >, however, I again receive numerous errors; this time starting with: error: no type named ‘proto_base_expr’. How can I define my own lambda arguments?

List stl C++ Struct

I am having a problem with the while loop inserting data into a stl list from text file. Could you please help me understand my errors? Thanks a lot.
Errors are
server3.cpp: In function ‘int main(int, char**)’:
server3.cpp:43:11: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
server3.cpp:74:15: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:74:25: error: ‘class std::list<Record>’ has no member named ‘firstName’
server3.cpp:74:42: error: ‘class std::list<Record>’ has no member named ‘lastName’
server3.cpp:75:12: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:76:17: error: no match for ‘operator[]’ in ‘hashtable[hash]’
server3.cpp:76:50: error: expected primary-expression before ‘)’ token
Code follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <vector>
#include <list>
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE =100;/*size of hashTable*/
/*Struct representing the record*/
struct Record
{
int id;
char firstName[100];
char lastName[100];
} rec;
/*Structure representing a single cell*/
class Cell
{
std:: list<Record> recs;
pthread_mutex_t lock;
};
/* The actual hash table */
std::list<Cell> hashtable;
int main (int argc, char * argv[])
{
ifstream indata; /* indata is like a cin*/
indata.open("fileName"); /* opens the file*/
list <Record> rec;/*create an object*/
int hash;
while ( !indata.eof() ) /* keep reading until end-of-file*/
{
indata>> rec.id >> rec.firstName >> rec.lastName;
hash =rec.id % sizeof(hashtable);
hashtable [hash].listofrecords.push_back (Record);
}
indata.close();
return 0;
}
Most of them are telling you that list doesn't have no members, because you try to do
indata>> rec.id >> rec.firstName >> rec.lastName;
but rec is a list, not a Record.
hashtable[hash]
is also illegal (see the interface for std::list, and Record is a type, and you can't insert a type in a container, you can only insert objects:
...push_back (Record);
is illegal.
The code doesn't have just the occasional error we all make from time to time, but is fundamentally flawed. I suggest you start learning C++ (if that's what you're doing) from a good book.
Please note that you are creating a collection of records, but this means that you need to access an element of the collection before accessing the fields of the records contained there.
Here's a reference on using the list type:
http://www.cplusplus.com/reference/list/list/
There are at least three major problems with this line alone.
hashtable [hash].listofrecords.push_back (Record);
std::list doesn't have an operator[] so you can't use the [hash] subscripting.
No place in your program have you defined what listofrecords means.
You are trying to push_back a type named Record where an object is required.
Please find a good C++ book to get started with: The Definitive C++ Book Guide and List

C++: 'set' and 'vector' "undeclared despite #include statements

I am using Netbeans 7.1 on Ubuntu 11.04.
The following call
set< Triangle > V;
gives the error message
error: ‘set’ was not declared in this scope
and the following call
vector< Triangle > ans;
gives the error message
error: ‘vector’ was not declared in this scope
This despite my having
#include <vector>
#include <set>
#include <map>
at the beginning of the C++ file.
At help resolving this would be greatly appreciated.
Peter.
Vectors Sets and map are part of the c++ Standard Library so you need to call vector/set/map with
std::vector< Triangle > ans;
or add
using namespace std;
after the include statements.
you forgot about namespace std :
std::set< Triangle > V;
std::vector< Triangle > V;
They live in the std namespace. So, either fully quality the types (std::vector) or use a using statement (using namespace std;).
The latter option pollutes the global namespace. Never do that in a header file (otherwise the entire namespace is imported when you include the header) and only do it in your implementation file if you know that it isn't going to cause any collisions.
#include <vector>
int main(...) {
vector v; // no worky
std::vector v; // ok!
}

include file error in C++

Problem fixed! Thanks a lot for the constructive suggestions!
I am unable to figure out what is the mistake in the following code. Is there something wrong with the way I am doing includes?
// This is utils.h
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef stack<int> si;
typedef queue<int> qi;
#define tr(c,i) for(typeof((c).begin()) i = (c).begin() ; i!=(c).end() ; ++i )
#define all(c) (c).begin(),(c).end()
#define cpresent(c,x) (find(all(c),x) != (c).end())
#endif
// ==============================================================
// Below is main.cpp
#include "utils.h"
int main() {
vi v;
}
On compiling "g++ main.cpp" I get the following error message:
utils.h:13: error: expected initializer before ‘<’ token
utils.h:14: error: expected initializer before ‘<’ token
utils.h:15: error: expected initializer before ‘<’ token
utils.h:16: error: expected initializer before ‘<’ token
utils.h:17: error: expected initializer before ‘<’ token
utils.h:18: error: expected initializer before ‘<’ token
main1.cpp: In function ‘int main()’:
main1.cpp:4: error: ‘vi’ was not declared in this scope
main1.cpp:4: error: expected `;' before ‘v’
What is wrong with this code? The utils.h used to work fine some time back when I did not have the #ifndefs in it.
Those types (pair, stack, queue, vector, etc.) are in the std namespace. You either need to add using namespace std; at the top of your file (generally after all of the standard library includes) or fully qualify the type names by adding std:: in front of them.
Generally, it's better practice to fully qualify the type names than to use using namespace to avoid potential collisions between names and to make your code cleaner. You should never use using namespace std in header files.
(Along the lines of clean code, you should consider using better, longer names for your types; ii, vii, and vvii are atrocious type names).
vector and the like are contained in the namespace std::. Do not use using namespace std; in a header file. Otherwise everyone that includes it gets all of std:: whether intended or not.
On a side note, if this is a utility header intended to be included in other files, you might wrap up those types and #define's in a namespace. Note #define's don't respect namespaces, so you'd prefix them instead:
namespace utility
{
// ...
typedef std::queue<int> qi;
// most would recommend this be in CAPS
#define utility_tr(c,i) for(typeof((c).begin()) i = (c).begin() ; i!=(c).end() ; ++i )
// ...
}
Before your typedefs, you should have using namespace std;
Also, you may wish to use a less common name than UTILS_H.
you should write
using namespace std;
before line
typedef pair<int,int> ii;
or fully qualify types of stl with std::