map problem, I'm not a student

B

Billy Patton

First, I'm not a student looking for help. Although there's nothing wrong
with studens looking for help here.
I'm learning c++ move further away from the perl wars here at
work (my version is better, use it only)

I've had this HashTable written in c that I've used for 10+ years. It works
without any problems. I'm trying to reduce my overall amount of code by using
STL in c++. To make migration easier I've create a class that inherits the map
class and made the whole thing into a template. I've kept my original function
calls to make my life easier.

Here's my efforts so far :
template <typename K,typename V> class HashTable: public std::map<K,V>
{
public:
bool Put(K&,V&,bool replace=false)
{
if (replace)
this->erase(K); <<<<<<< LINE# 79
return (this->insert(make_pair(K,V))) ? true : false;
}
V& Value(K&) { }
bool Keys(K&,bool firstp = false) { }
bool Exists(K&) { }
void DumpKeys(void) { }
unsigned int EntryCount(void) { return this->size(); }
};


My test_script so far :
#include "include/HashTable.h"
#include <string>
using namespace std;
int main(void)
{
HashTable<std::string,std::string> shash;
HashTable<int,int> ihash;
HashTable<char*,char*> chash;

return 0;
}


The results of my compile:
[bpatton@holster07 cdmg_toolbox]$ g++ -o x x.cxx
In file included from x.cxx:1:
include/HashTable.h: In member function `bool HashTable<K, V>::put(K&, V&,
bool)':
include/HashTable.h:79: error: expected primary-expression before ')' token
include/HashTable.h:80: error: expected primary-expression before ',' token
include/HashTable.h:80: error: expected primary-expression before ')' token
include/HashTable.h:80: error: there are no arguments to `make_pair' that
depend on a template parameter, so a declaration of `make_pair' must be
available
include/HashTable.h:80: error: (if you use `-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
V

Victor Bazarov

Billy said:
First, I'm not a student looking for help. Although there's nothing
wrong with studens looking for help here.

No, there is nothing wrong, unless they just want others to do their job
for them.
I'm learning c++ move further away from the perl wars here at
work (my version is better, use it only)

I've had this HashTable written in c that I've used for 10+ years. It
works without any problems. I'm trying to reduce my overall amount of
code by using STL in c++. To make migration easier I've create a class
that inherits the map class and made the whole thing into a template.
I've kept my original function calls to make my life easier.

Here's my efforts so far :
template <typename K,typename V> class HashTable: public std::map<K,V>
{
public:
bool Put(K&,V&,bool replace=false)

bool Put(K& k, V& v, bool replace = false)
{
if (replace)
this->erase(K); <<<<<<< LINE# 79

this->erase(k);

You can't erase a type. You need a value. That's what the compiler is
suggesting ("expected primary-expression").
return (this->insert(make_pair(K,V))) ? true : false;

std::make_pair(k,v)

and, did you include the necessary headers? Also, I don't think that
the return value of 'insert' has conversion to 'bool' defined, you
probably want to do

return this->insert(std::make_pair(k,v)).second;
}
V& Value(K&) { }

That is not valid C++. A function that returns V& should have at least
one 'return' statement.
bool Keys(K&,bool firstp = false) { }

Same here. Is this real code or is this a joke?
bool Exists(K&) { }
???

void DumpKeys(void) { }
unsigned int EntryCount(void) { return this->size(); }
};


My test_script so far :
#include "include/HashTable.h"
#include <string>
using namespace std;
int main(void)
{
HashTable<std::string,std::string> shash;
HashTable<int,int> ihash;
HashTable<char*,char*> chash;

return 0;
}


The results of my compile:
[bpatton@holster07 cdmg_toolbox]$ g++ -o x x.cxx
In file included from x.cxx:1:
include/HashTable.h: In member function `bool HashTable<K, V>::put(K&,
V&, bool)':
include/HashTable.h:79: error: expected primary-expression before ')' token
include/HashTable.h:80: error: expected primary-expression before ',' token
include/HashTable.h:80: error: expected primary-expression before ')' token
include/HashTable.h:80: error: there are no arguments to `make_pair'
that depend on a template parameter, so a declaration of `make_pair'
must be available
include/HashTable.h:80: error: (if you use `-fpermissive', G++ will
accept your code, but allowing the use of an undeclared name is deprecated)

V
 
M

Mike Wahler

Billy Patton said:
First, I'm not a student looking for help. Although there's nothing wrong
with studens looking for help here.
I'm learning c++ move further away from the perl wars here at
work (my version is better, use it only)

I've had this HashTable written in c that I've used for 10+ years. It works
without any problems. I'm trying to reduce my overall amount of code by using
STL in c++. To make migration easier I've create a class that inherits the map
class and made the whole thing into a template. I've kept my original function
calls to make my life easier.

Here's my efforts so far :
template <typename K,typename V> class HashTable: public std::map<K,V>
{
public:
bool Put(K&,V&,bool replace=false)
{
if (replace)
this->erase(K); <<<<<<< LINE# 79

'K' is a type, not an object. E.g. if 'K' is 'int',
you have:

this->erase(int);

which doesn't make much sense, does it? :)

Perhaps you want to give names to your first two parameters
and use them.
return (this->insert(make_pair(K,V))) ? true : false;

Same thing here.

-Mike
 
D

David Crocker

Billy Patton said:
First, I'm not a student looking for help. Although there's nothing wrong
with studens looking for help here.
I'm learning c++ move further away from the perl wars here at
work (my version is better, use it only)

I've had this HashTable written in c that I've used for 10+ years. It works
without any problems. I'm trying to reduce my overall amount of code by using
STL in c++. To make migration easier I've create a class that inherits the map
class and made the whole thing into a template. I've kept my original function
calls to make my life easier.

Here's my efforts so far :
template <typename K,typename V> class HashTable: public std::map<K,V>
{
public:
bool Put(K&,V&,bool replace=false)
{
if (replace)
this->erase(K); <<<<<<< LINE# 79
return (this->insert(make_pair(K,V))) ? true : false;
}
V& Value(K&) { }
bool Keys(K&,bool firstp = false) { }
bool Exists(K&) { }
void DumpKeys(void) { }
unsigned int EntryCount(void) { return this->size(); }
};


My test_script so far :
#include "include/HashTable.h"
#include <string>
using namespace std;
int main(void)
{
HashTable<std::string,std::string> shash;
HashTable<int,int> ihash;
HashTable<char*,char*> chash;

return 0;
}


The results of my compile:
[bpatton@holster07 cdmg_toolbox]$ g++ -o x x.cxx
In file included from x.cxx:1:
include/HashTable.h: In member function `bool HashTable<K, V>::put(K&, V&,
bool)':
include/HashTable.h:79: error: expected primary-expression before ')' token
include/HashTable.h:80: error: expected primary-expression before ',' token
include/HashTable.h:80: error: expected primary-expression before ')' token
include/HashTable.h:80: error: there are no arguments to `make_pair' that
depend on a template parameter, so a declaration of `make_pair' must be
available
include/HashTable.h:80: error: (if you use `-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)

I think you really meant something like this:

bool Put(K& k,V& v,bool replace=false)
{
if (replace)
this->erase(k);
return (this->insert(make_pair(k,v))) ? true : false;
}

BTW your return expression "(this->insert(make_pair(k,v))) ? true : false"
is a long-winded way of saying "this->insert(make_pair(k,v))".

David
 
B

Billy Patton

No, there is nothing wrong, unless they just want others to do their job
for them.


bool Put(K& k, V& v, bool replace = false)


this->erase(k);

You can't erase a type. You need a value. That's what the compiler is
suggesting ("expected primary-expression").


std::make_pair(k,v)

and, did you include the necessary headers? Also, I don't think that
the return value of 'insert' has conversion to 'bool' defined, you
probably want to do

return this->insert(std::make_pair(k,v)).second;


That is not valid C++. A function that returns V& should have at least
one 'return' statement.

THese are unfinished code. just have {} to make compiler happy
bool Keys(K&,bool firstp = false) { }

Same here. Is this real code or is this a joke?
bool Exists(K&) { }
???

void DumpKeys(void) { }
unsigned int EntryCount(void) { return this->size(); }
};


My test_script so far :
#include "include/HashTable.h"
#include <string>
using namespace std;
int main(void)
{
HashTable<std::string,std::string> shash;
HashTable<int,int> ihash;
HashTable<char*,char*> chash;

return 0;
}


The results of my compile:
[bpatton@holster07 cdmg_toolbox]$ g++ -o x x.cxx
In file included from x.cxx:1:
include/HashTable.h: In member function `bool HashTable<K, V>::put(K&, V&,
bool)':
include/HashTable.h:79: error: expected primary-expression before ')' token
include/HashTable.h:80: error: expected primary-expression before ',' token
include/HashTable.h:80: error: expected primary-expression before ')' token
include/HashTable.h:80: error: there are no arguments to `make_pair' that
depend on a template parameter, so a declaration of `make_pair' must be
available
include/HashTable.h:80: error: (if you use `-fpermissive', G++ will accept
your code, but allowing the use of an undeclared name is deprecated)

V

___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
B

Billy Patton

THanks for the assistance. I knew it had to be something simple like leveing
off the varaible.

Not to the next problem. I have:
V& Value(K& v)
{
std::map<K,V>::iterator iter = this->find(v); <<< LINE# 23
if( iter != this->end() )
return iter->second;
return NULL;
}

THe compiler complains about :
x.cxx: In member function `V& HashTable<K, V>::Value(K&)':
x.cxx:23: error: expected `;' before "iter"
x.cxx:24: error: `iter' undeclared (first use this function)
x.cxx:24: error: (Each undeclared identifier is reported only once for each
function it appears in.)


I pulled this almost word for word exactly from docs online.
begin
Syntax:
find
Syntax:

iterator find( const KEY_TYPE &key );

The find() function returns an iterator to key, or an iterator to the end of
the map if key is not found. For example, the following code searches a map for
a specific character and displays its associated ASCII value:

map<char,int> characterMap;

for( int i = 0; i < 26; i++ ) {
characterMap.insert( pair<char,int>('A'+i, 65+i) );
characterMap.insert( pair<char,int>('a'+i, 97+i) );
}

char ch;
cout << "Enter a character: ";
cin >> ch;

map<char,int>::iterator iter = characterMap.find(ch);
if( iter != characterMap.end() ) {
cout << "ASCII value: " << iter->second << endl;
} else {
cout << "Character " << ch << " not found" << endl;
}


So what is wrong ?
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
B

Billy Patton

Here's what I have:
bool Keys(K& k,bool firstp = false)
{
typedef typename std::map<K,V>::iterator map_iter;
static map_iter iter = this->begin();
if (iter EQ this->end()) return false;
k = iter->first();
++iter;
return true;
}

I had to add the typedef and the typename to get it to work.
Why?


___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
V

Victor Bazarov

Billy said:
THanks for the assistance. I knew it had to be something simple like
leveing off the varaible.

Not to the next problem. I have:
V& Value(K& v)
{
std::map<K,V>::iterator iter = this->find(v); <<< LINE# 23

You need

typename std::map<K,V>::iterator iter = this->find(v);

'iterator' is a dependent type. In order for the compiler to use it as it
usually would in a declaration, you need to give it a hint by using the
'typename' keyword.
if( iter != this->end() )
return iter->second;
return NULL;
}

THe compiler complains about :
x.cxx: In member function `V& HashTable<K, V>::Value(K&)':
x.cxx:23: error: expected `;' before "iter"
x.cxx:24: error: `iter' undeclared (first use this function)
x.cxx:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
[...]

V
 
V

Victor Bazarov

Billy said:
Here's what I have:
bool Keys(K& k,bool firstp = false)
{
typedef typename std::map<K,V>::iterator map_iter;
static map_iter iter = this->begin();
if (iter EQ this->end()) return false;
k = iter->first();
++iter;
return true;
}

I had to add the typedef and the typename to get it to work.
Why?

You didn't need that. You could simply do

typename std::map<K,V>::iterator iter = this->begin;

BTW, you do NOT want it static, otherwise it will only be initialised
once, and next time you run, it will retain the value it got from the
previous call to 'Keys'.

V
 
M

Matthias =?ISO-8859-1?Q?K=E4ppler?=

First of all, when looking at your code, I have the impression you don't
understand what a reference is in C++. You declare your function returning
'V' references, however, on failure you return NULL.

NULL in C++ is *not* the "empty" reference. In fact in C++ there is no such
thing as a "NULL-reference". References ain't pointers. Don't confuise them
with the way Java references are handled. In C++, a reference is merely an
alias for another object. It's basically like having two names to access
the same object.
So unless you want to return references to pointers, which is certainly a
dubious practice, you're in trouble.
OTOT, Since NULL is usually defined as 0 or (void*)0, I don't even try to
imagine what will happen when you instantiate your template with an invalid
type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top