map problem, I'm not a student

Discussion in 'C++' started by Billy Patton, Dec 7, 2004.

  1. Billy Patton

    Billy Patton Guest

    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,
     
    Billy Patton, Dec 7, 2004
    #1
    1. Advertising

  2. Billy Patton wrote:
    > 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
     
    Victor Bazarov, Dec 7, 2004
    #2
    1. Advertising

  3. Billy Patton

    Mike Wahler Guest

    "Billy Patton" <> wrote in message
    news:p...
    > 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
     
    Mike Wahler, Dec 7, 2004
    #3
  4. "Billy Patton" <> wrote in message
    news:p...
    > 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,


    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
     
    David Crocker, Dec 7, 2004
    #4
  5. Billy Patton

    Billy Patton Guest

    On Tue, 7 Dec 2004, Victor Bazarov wrote:

    > Billy Patton wrote:
    >> 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.


    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,
     
    Billy Patton, Dec 7, 2004
    #5
  6. Billy Patton

    Billy Patton Guest

    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,
     
    Billy Patton, Dec 7, 2004
    #6
  7. Billy Patton

    Billy Patton Guest

    Re: map problem, Got it!

    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,
     
    Billy Patton, Dec 7, 2004
    #7
  8. Billy Patton wrote:
    > 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
     
    Victor Bazarov, Dec 7, 2004
    #8
  9. Re: map problem, Got it!

    Billy Patton wrote:
    > 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
     
    Victor Bazarov, Dec 7, 2004
    #9
  10. 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.

    Billy Patton wrote:
    > 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.)
     
    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Dec 7, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JavaMan
    Replies:
    1
    Views:
    357
    Lee Weiner
    Oct 30, 2004
  2. judith
    Replies:
    5
    Views:
    356
    schouery
    Oct 16, 2006
  3. Replies:
    6
    Views:
    321
  4. Replies:
    6
    Views:
    310
    Daniel T.
    Nov 27, 2006
  5. rgt

    Student Banking Problem

    rgt, Apr 11, 2008, in forum: Java
    Replies:
    10
    Views:
    689
    Roedy Green
    Apr 24, 2008
Loading...

Share This Page