LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   about STL programming (https://www.linuxquestions.org/questions/programming-9/about-stl-programming-304878/)

wangjinyi 03-22-2005 07:06 PM

about STL programming
 
that is the cpp and h file below:
<code>
struct person {
int age;
char name[10];
};

struct GetName {
char* operator()(person one) const { return one.name; }
};

struct EqualKey {
bool operator()(char* c1, char* c2) const { return strcmp(c1, c2) == 0; }
};

int main(void)
{
hashtable<person, char*, hash<char*>, GetName, EqualKey> stuff;
}

#ifndef __HASHTEST_H__
#define __HASHTEST_H__

#include </usr/include/c++/3.2.2/ext/stl_hashtable.h>

#endif
</code>
and, when compile it, output is below:

g++ -g -I. -I/usr/include/c++/3.2.2/ext hashtest.cpp -o mytest -L.
hashtest.cpp: In function `int main()':
hashtest.cpp:19: `hashtable' undeclared (first use this function)
hashtest.cpp:19: (Each undeclared identifier is reported only once for each
function it appears in.)
hashtest.cpp:19: parse error before `,' token
make: *** [all] Error 1


I am sure the exactly h file stl_hashtable.h be in there.
and when i put a key work class before the word hashtable in
function main, the output change to below:

g++ -g -I. -I/usr/include/c++/3.2.2/ext hashtest.cpp -o mytest -L.
hashtest.cpp: In function `int main()':
hashtest.cpp:19: `hash' undeclared (first use this function)
hashtest.cpp:19: (Each undeclared identifier is reported only once for each
function it appears in.)
hashtest.cpp:19: parse error before `*' token
make: *** [all] Error 1

I need your help. anything is helpful.

YetAnotherDave 03-24-2005 08:34 AM

"hashtable" is in the __gnu_cxx namespace. You can use "using namespace __gnu_cxx;" to let the compiler know to look there for "hashtable". Also, there is no default constructor for hashtable<...> so you need to specifiy some arguments to "stuff()" as shown below.

The modified version of your program shown below compiles and links ( at least it did for me but I'm using (gcc 3.3) which is different from the version you are using ( gcc 3.2.2 ).


- Dave

Code:

using namespace __gnu_cxx;

struct person {
    int age;
    char name[10];
};

struct GetName {
    char* operator()(person one) const { return one.name; }
};

struct EqualKey {
    bool operator()(char* c1, char* c2) const { return strcmp(c1, c2) == 0; }
};

int main(void)
{
    hash<char*> fcn;
    EqualKey key;
    hashtable<person, char*, hash<char*>, GetName, EqualKey> stuff(0, fcn, key);
}


wangjinyi 03-24-2005 05:45 PM

That's the point.
Thanks a lot.


All times are GMT -5. The time now is 07:09 PM.