Sign in to follow this  

[solved] STL hash_map problems

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks, I am having some trouble with using the GNU version of the STL hash_map. I am using gcc 3.3.4 on Debian (last dist-upgrade done today). The following code snippet produces a "syntax error before `;' token" in the marked line when trying to compile it: [...] #include <string> #include <ext/hash_map> #include <OpenSG/OSGImage.h> class SS_BitmapFont { protected: // --- error here --- std::hash_map<std::string, osg::ImagePtr> glyphMap; // ------------------ unsigned short glyphWidth; unsigned short glyphHeight; [...] }; [...] Do you have any ideas what might be the problem here? I can't see any syntax error in the specified line. Could there be a problem in the GNU version of the "hash_map" header file? And if there really is a problem with this file, do you have any suggestions what I can do about it besides writing my own hash_map implementation? The original STL headers from SGI don't work on Debian (already tried that earlier today). Thanks in advance for any hints. [Edited by - soeren_schupp on September 15, 2004 3:35:37 AM]

Share this post


Link to post
Share on other sites
first, when you say debian, say debian testing. in this case I could infer from gcc 3.3, but...

second, show a complete file that causes the error, and show the error. then I might be able to help.

Share this post


Link to post
Share on other sites
OK, I am using Debian/unstable (maybe that's the problem already ;-) ). What gcc version would you recommend?

So here comes the complete code of my header file and the complete compile log:

SS_BitmapFont.h
---------------



#ifndef SS_BITMAP_FONT_H
#define SS_BITMAP_FONT_H

#include <string>
#include <ext/hash_map>
#include <OpenSG/OSGImage.h>

class SS_BitmapFont
{
protected:

std::hash_map<std::string, osg::ImagePtr> glyphMap;
unsigned short glyphWidth;
unsigned short glyphHeight;

public:

// c'tor
SS_BitmapFont(std::string fontBitmapPath,
unsigned short glyphW ,
unsigned short glyphH ,
unsigned short glyphSpacing );

// generate image from given string
osg::ImagePtr generateImageFromString(std::string text);
};

#endif




SS_BitmapFont.cpp
-----------------
I'll post this only if you really need it because it has over 1600 lines of code and I really don't think the problem originates from that file.


compilation
-----------

g++ -c SS_BitmapFont.cpp -march=i486 -O5
In file included from SS_BitmapFont.cpp:6:
SS_BitmapFont.h:16: error: syntax error before `;' token
SS_BitmapFont.cpp: In constructor `
SS_BitmapFont::SS_BitmapFont(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, short unsigned int, short unsigned int, short
unsigned int)':
SS_BitmapFont.cpp:32: error: `glyphMap' undeclared (first use this function)
SS_BitmapFont.cpp:32: error: (Each undeclared identifier is reported only once
for each function it appears in.)
make: *** [SS_BitmapFont.o] Error 1

[Edited by - soeren_schupp on September 14, 2004 6:56:56 PM]

Share this post


Link to post
Share on other sites
hmmm. ext/ ??

do a 'which hash_map'

and then use whatever path it gives you and run it through

dpkg -S /path/to/hash_map

tell me what that says.

on my debian testing machine (not THAT different from unstable, especially with the freezes in place) I have no ext/

Share this post


Link to post
Share on other sites
There are two things you need to know about hash_map on gcc, the first is its in namespace __gnu_cxx, secondly you need to do specialization of hash for std::strings if your using std::string as a key.

I've written some code that let you use hash_map on gcc & VS.NET 7.1 plus some other compilers aswell. It detects if gcc is being used if it is then includes the header, does a specialization for std::strings and then makes a namespace alias for __gnu_cxx as ext, if its not gcc then it just makes a namespace alias for std as ext so not matter what it will compile with out you changing any code.


#ifdef __GNUC__
# include <cstddef>
# include <string>
# include <ext/hash_map>
namespace __gnu_cxx {

template<>
struct hash< std::string > {
size_t operator()(const std::string& s) const {
const std::collate< char >& c = std::use_facet< std::collate< char > >(std::locale::classic());
return c.hash(s.c_str(), s.c_str() + s.size());
}
};

template<>
struct hash< const std::string > {
size_t operator()(const std::string& s) const {
const std::collate< char >& c = std::use_facet< std::collate< char > >(std::locale::classic());
return c.hash(s.c_str(), s.c_str() + s.size());
}
};
}
namespace ext = __gnu_cxx;
#else
# include <string>
# include <locale>
# include <hash_map>
namespace ext = std;
#endif




example use:


#ifdef __GNUC__
# include <cstddef>
# include <string>
# include <ext/hash_map>
namespace __gnu_cxx {

template<>
struct hash< std::string > {
size_t operator()(const std::string& s) const {
const std::collate< char >& c = std::use_facet< std::collate< char > >(std::locale::classic());
return c.hash(s.c_str(), s.c_str() + s.size());
}
};

template<>
struct hash< const std::string > {
size_t operator()(const std::string& s) const {
const std::collate< char >& c = std::use_facet< std::collate< char > >(std::locale::classic());
return c.hash(s.c_str(), s.c_str() + s.size());
}
};
}
namespace ext = __gnu_cxx;
#else
# include <string>
# include <locale>
# include <hash_map>
namespace ext = std;
#endif

int main() {

ext::hash_map< std::string, int > m;

return 0;

}




[Edited by - snk_kid on November 28, 2004 11:52:19 AM]

Share this post


Link to post
Share on other sites

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this