Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11
  1. #1

    [C++]problema con iteratore di hash_map

    Ciao a tutti.
    sto facendo il porting per LINUX di una piccola libreria per il motore grafico OGRE: http://www.ogre3d.org/wiki/index.php...mmonFileDialog .

    Serve a creare una finestra per caricare files.

    Il problema è nel file CEGUIHashMapTemplates.h dove dichiara l'iteratore:

    codice:
    	template<typename Z, class T>
    	T getEntryFromHashMap (Z id, __gnu_cxx::hash_map<Z, T>* hashMap)
    	{
    		// Search the entry in the hashMap
    		if (hashMap)
    		{
    			__gnu_cxx::hash_map<Z, T>::iterator hashMapIterator;
    			hashMapIterator = hashMap->find (id);
    			if (hashMapIterator != hashMap->end())
    			{
    				// Found it
    				return hashMapIterator->second;
    			}
    		}
    
    		return NULL;
    	};
    la riga dove viene dichiarato non viene accettata e dice :
    "expected ';' before 'hashMapIterator' " .


    Non capisco come mai sinceramente. Su windows funziona corrtettamente.
    La definizione di questa estensione si trova in /usr/include/c++/4.1.3/ext .

    Avete qualche idea per risolvere il problema? il codice dovrebbe essere questo: http://www.projectblackdog.com/3rdPa...ap-source.html
    rm -f stupidity

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381

    Re: [C++]problema con iteratore di hash_map

    Prova mettere un typename
    codice:
    	template<typename Z, class T>
    	T getEntryFromHashMap (Z id, __gnu_cxx::hash_map<Z, T>* hashMap)
    	{
    		// Search the entry in the hashMap
    		if (hashMap)
    		{
    			typename __gnu_cxx::hash_map<Z, T>::iterator hashMapIterator;
    			hashMapIterator = hashMap->find (id);
    			if (hashMapIterator != hashMap->end())
    			{
    				// Found it
    				return hashMapIterator->second;
    			}
    		}
    
    		return NULL;
    	};

  3. #3
    hei, compila!
    grazie !

    come mai? mi spieghi cos'è che aggiunge quel typename ?
    rm -f stupidity

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Semplicemente specifica che

    __gnu_cxx::hash_map<Z, T>::iterator

    non è una variabile membro di hash_map ma un tipo.

    g++ se non ricordo male è piuttosto pignolo (a ragione) su questo punto.

  5. #5
    ah ok.

    grazie ancora.
    Happy New Year
    rm -f stupidity

  6. #6
    Ciao.
    Sto incontrando un altro problema in questo porting...
    quando il compilatore ( g++ 4.1.3 ) arriva a questa riga (file CEGUICommonFileDialog.cpp) :

    codice:
    	CommonFileDialog::_InstPtr CommonFileDialog::sm_ptr;
    mi da errore: " too few template-parameter-lists " .

    CommonFileDialog è un typedef, definito alla fine di CEGUICommonFileDialog.h :
    codice:
    	typedef CEGUISingletonX<CCommonFileDialog> CommonFileDialog;



    Ho googlato un bel po' ma non sono riuscito a capire esattamente il problema (causa anche della mia inesperienza coi template). Sembra che sia un problema con uno static member. Si riferisce a questa classe (CEGUISingletonX.h) :

    codice:
    	template<class T>
    	class CEGUISingletonX
    	{
    		private:
    			class _InstPtr
    			{
    				public:
    					_InstPtr() : m_ptr(0) {}
    					~_InstPtr() { delete m_ptr; }
    					T* get() { return m_ptr; }
    					void Set(T* p)
    					{
    			            		if(p!= 0)
    					        {
    							delete m_ptr;
    			                		m_ptr = p;
    					        }
    			        	}
    			    private:
    					T* m_ptr;
    			};
    
    			static _InstPtr sm_ptr;
    			CEGUISingletonX();
    			CEGUISingletonX(const CEGUISingletonX&);
    			CEGUISingletonX& operator=(const CEGUISingletonX&);
    
    		public:
    			static T& getSingleton()
    			{
    				if(sm_ptr.get() == 0)
    				{
    		            		sm_ptr.Set(new T());
    		        	}
    				return *sm_ptr.get();
    			}
    
    			static T* getSingletonPtr()
    			{
    				if(sm_ptr.get() == 0)
    				{
    		        		sm_ptr.Set(new T());
    		        	}
    				return sm_ptr.get();
    			}
    	};
    }
    dove sono dichiarati _InstPtr e sm_ptr;

    Non so più dove sbattere la testa sinceramente : )
    rm -f stupidity

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Manca la definizione della variabile sm_ptr che in quanto static DEVE essere esterna alla dichiarazione della classe.
    Inoltre essendo la classe un template, la variabile DEVE essere indicata nel template.
    Il typedef eventualmente va fatto dopo.

    Riporto in rosso sotto.
    codice:
    	template<class T>
    	class CEGUISingletonX
    	{
    		private:
    			class _InstPtr
    			{
    				public:
    					_InstPtr() : m_ptr(0) {}
    					~_InstPtr() { delete m_ptr; }
    					T* get() { return m_ptr; }
    					void Set(T* p)
    					{
    			            		if(p!= 0)
    					        {
    							delete m_ptr;
    			                		m_ptr = p;
    					        }
    			        	}
    			    private:
    					T* m_ptr;
    			};
    
    			static _InstPtr sm_ptr;
    			CEGUISingletonX();
    			CEGUISingletonX(const CEGUISingletonX&);
    			CEGUISingletonX& operator=(const CEGUISingletonX&);
    
    		public:
    			static T& getSingleton()
    			{
    				if(sm_ptr.get() == 0)
    				{
    		            		sm_ptr.Set(new T());
    		        	}
    				return *sm_ptr.get();
    			}
    
    			static T* getSingletonPtr()
    			{
    				if(sm_ptr.get() == 0)
    				{
    		        		sm_ptr.Set(new T());
    		        	}
    				return sm_ptr.get();
    			}
    	};
            template <class T>
            CEGUISingletonX<T>::_InstPtr CEGUISingletonX<T>::sm_ptr;
    
            typedef CEGUISingletonX<CCommonFileDialog> CommonFileDialog;
    
    

  8. #8
    Putroppo quando aggiungo quella riga in rosso mi da un altro errore :
    codice:
    "error: expected constructor, destructor, or type conversion before 'CEGUISingletonX' " .
    Cosa significa?

    Il tipo che l'ha scritta dice di aver usato Visual Studio. Mi pare di capire che il compilatore Microsoft sia un POCHINO più permissivo del g++ : )
    rm -f stupidity

  9. #9
    Originariamente inviato da FUN-BOY Mi pare di capire che il compilatore Microsoft sia un POCHINO più permissivo del g++ : )
    Più errori in fase di compilazione = meno errori runtime
    ;-)

  10. #10
    Originariamente inviato da MacApp
    Più errori in fase di compilazione = meno errori runtime
    ;-)
    mi sa che hai proprio ragione ; )


    Comunque, ho risolto il problema sopra: bastava mettere "typename" davanti a CEGUISingletonX. Evviva.

    Ovviamente mi da altri errori adesso
    Pare infatti che usare la ext/hash_map abbia confuso il compilatore visto che viene usata anche nel motore grafico. g++ mi dice:

    codice:
    /usr/local/include/OGRE/OgreString.h:40: error: specialization of ‘__gnu_cxx::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ after instantiation
    /usr/local/include/OGRE/OgreString.h:40: error: redefinition of ‘struct __gnu_cxx::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’
    /usr/include/c++/4.1.3/ext/hash_fun.h:71: error: previous definition of ‘struct __gnu_cxx::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’
    quindi non capisce più che funzione di hash usare mi sembra... è un errore assurdo perchè OGRE è stato compilato correttamente. Ad ogni modo quel benedetto OgreString.h contiene questo:
    codice:
    #include <ext/hash_map>
    namespace __gnu_cxx
    {
        template <> struct hash< Ogre::_StringBase >
        {
            size_t operator()( const Ogre::_StringBase _stringBase ) const
            {
                /* This is the PRO-STL way, but it seems to cause problems with VC7.1
                   and in some other cases (although I can't recreate it)
                hash<const char*> H;
                return H(_stringBase.c_str());
                */
                /** This is our custom way */
                register size_t ret = 0;
                for( Ogre::_StringBase::const_iterator it = _stringBase.begin(); it != _stringBase.end(); ++it )
                    ret = 5 * ret + *it;
    
                return ret;
            }
        };
    }
    
    #endif
    
    namespace Ogre {
    
        /** Utility class for manipulating Strings.  */
        class _OgreExport StringUtil
        {
    ... .
    ...
    ETC ETC
    Sono a un passo da aver finito il porting accidenti !
    rm -f stupidity

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.