Thread: HELP PLEASE: I can't find the error

  1. #1
    Baal reivaj7999's Avatar
    Join Date
    Aug 2005
    Posts
    3

    Unhappy HELP PLEASE: I can't find the error

    Hello everyone, how do you do?

    I do not know where is the error in this code, I am currently working with C++ Builder 6.
    C++ Builder marks an error where in the bold letters, this is the following error:
    [C++ Error] test.cpp(16): E2139 Declaration missing ;

    I do not know what to do !!!
    Help please!!!
    Regards Reivaj.
    Code:
    #include<math>
    #include<stddef>
    #include<memory>
    #include<cstring>
    #include<bitset>
     
    #define TABLE_SIZE 128
    #define Allocator allocator
    template <class Key, class T>
    class Hashtable
    {
    public:
    	typedef Allocator<T> hashtable_allocator;
    	typedef typename hashtable_allocator::reference reference;
    	typedef typename hashtable_allocator::const_reference const_reference;
    	typedef typename hashtable_allocator::pointer iterator;
    	typedef typename hashtable_allocator::const_pointer const_iterator;
    	typedef typename hashtable_allocator::size_type size_type;
    	typedef typename hashtable_allocator::difference_type difference_type;
    	typedef typename T value_type;
    	typedef typename Key key_type;
    protected:
    	float loadFactor;
    	size_t size, numItems, thresold;
    	Allocator<T> the_allocator;
    	iterator start, finish, end_of_storage;
    	inline unsigned hashValue(unsigned key)
    	{
    	 return (31*key)%size;
    	}
    	inline unsigned hashValue(unsigned long key)
    	{
    	 return hashValue( reinterpret_cast<unsigned>(key) );
    	}
    	inline unsigned hashValue(float key)
    	{
    	 unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
    	 bitset<32> bits(*keyRef);
    	 return hashValue( bits.to_ulong() );
    	}
    	inline unsigned hashValue(double key)
    	{
    	 unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
    	 bitset<32> lsBits(*keyRef);
    	 bitset<32> msBits(*(++keyRef));
    	 lsBits ^= msBits;
    	 return hashValue( lsBits.to_ulong() );
    	}
    	inline unsigned hashValue(string key)
    	{
    	 unsigned h=0;
    	 size_t len = key.length();
    	 for(unsigned i=0; i<len; i++)
    		h = 31*h + key[i];
    	 return h%size;
    	}
    	inline unsigned hashValue(key_type key)
    	{
    	 return hashValue( reinterpret_cast<unsigned>(&key) );
    	}
    public:
    	explicit Hashtable(size_t size, float loadFactor)
    	{
    	 this->size = size > 0 ? size:TABLE_SIZE;
    	 this->loadFactor = loadFactor;
    	 this->thresold = this->size * this->loadFactor;
    	 this->numItems = 0;
    	 start = the_allocator.allocate(this->size);
    	 T value;
    	 uninitialized_fill_n(start, this->size, value);
    	 finish = start + this->size;
    	 end_of_storage = finish;
    	}
    	explicit Hashtable(size_t size)
    	{
    	 this( size, 0.75 );
    	}
    	Hashtable(const Hashtable<T>& t)
    	{
    	 this->size = t.size;
    	 this->loadFactor = t.loadFactor;
    	 this->thresold = t.thresold;
    	 this->numItems = t.numItems;
    	 this->start = this->the_allocator.allocate(t.finish - t.start);
    	 uninitialized_copy(t.start, t.finish, this->start);
    	 this->end_of_storage = this->finish;
    	}
    	~Hashtable()
    	{
    	 destroy(start, finish);
    	 the_allocator.deallocate(start);
    	}
    	iterator begin(){ return start; }
    	const_iterator begin() const { return start; }
    	iterator end(){ return finish; }
    	const_iterator end() const { return finish; }
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    > #define Allocator allocator
    What is that line for?

    allocator is part of the std namespace, so you might want to use std::allocator.

    And shouldn't it be <cmath> and <cstddef>?

  3. #3
    Baal reivaj7999's Avatar
    Join Date
    Aug 2005
    Posts
    3
    Quote Originally Posted by reivaj7999
    Hello everyone, how do you do?

    I do not know where is the error in this code, I am currently working with C++ Builder 6.
    C++ Builder marks an error where in the bold letters, this is the following error:
    [C++ Error] test.cpp(16): E2139 Declaration missing ;

    I do not know what to do !!!
    Help please!!!
    Regards Reivaj.
    Code:
    #include<math>
    #include<stddef>
    #include<memory>
    #include<cstring>
    #include<bitset>
     
    #define TABLE_SIZE 128
    #define Allocator allocator
    template <class Key, class T>
    class Hashtable
    {
    public:
    	typedef Allocator<T> hashtable_allocator;
    	typedef typename hashtable_allocator::reference reference;
    	typedef typename hashtable_allocator::const_reference const_reference;
    	typedef typename hashtable_allocator::pointer iterator;
    	typedef typename hashtable_allocator::const_pointer const_iterator;
    	typedef typename hashtable_allocator::size_type size_type;
    	typedef typename hashtable_allocator::difference_type difference_type;
    	typedef typename T value_type;
    	typedef typename Key key_type;
    protected:
    	float loadFactor;
    	size_t size, numItems, thresold;
    	Allocator<T> the_allocator;
    	iterator start, finish, end_of_storage;
    	inline unsigned hashValue(unsigned key)
    	{
    	 return (31*key)%size;
    	}
    	inline unsigned hashValue(unsigned long key)
    	{
    	 return hashValue( reinterpret_cast<unsigned>(key) );
    	}
    	inline unsigned hashValue(float key)
    	{
    	 unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
    	 bitset<32> bits(*keyRef);
    	 return hashValue( bits.to_ulong() );
    	}
    	inline unsigned hashValue(double key)
    	{
    	 unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
    	 bitset<32> lsBits(*keyRef);
    	 bitset<32> msBits(*(++keyRef));
    	 lsBits ^= msBits;
    	 return hashValue( lsBits.to_ulong() );
    	}
    	inline unsigned hashValue(string key)
    	{
    	 unsigned h=0;
    	 size_t len = key.length();
    	 for(unsigned i=0; i<len; i++)
    		h = 31*h + key[i];
    	 return h%size;
    	}
    	inline unsigned hashValue(key_type key)
    	{
    	 return hashValue( reinterpret_cast<unsigned>(&key) );
    	}
    public:
    	explicit Hashtable(size_t size, float loadFactor)
    	{
    	 this->size = size > 0 ? size:TABLE_SIZE;
    	 this->loadFactor = loadFactor;
    	 this->thresold = this->size * this->loadFactor;
    	 this->numItems = 0;
    	 start = the_allocator.allocate(this->size);
    	 T value;
    	 uninitialized_fill_n(start, this->size, value);
    	 finish = start + this->size;
    	 end_of_storage = finish;
    	}
    	explicit Hashtable(size_t size)
    	{
    	 this( size, 0.75 );
    	}
    	Hashtable(const Hashtable<T>& t)
    	{
    	 this->size = t.size;
    	 this->loadFactor = t.loadFactor;
    	 this->thresold = t.thresold;
    	 this->numItems = t.numItems;
    	 this->start = this->the_allocator.allocate(t.finish - t.start);
    	 uninitialized_copy(t.start, t.finish, this->start);
    	 this->end_of_storage = this->finish;
    	}
    	~Hashtable()
    	{
    	 destroy(start, finish);
    	 the_allocator.deallocate(start);
    	}
    	iterator begin(){ return start; }
    	const_iterator begin() const { return start; }
    	iterator end(){ return finish; }
    	const_iterator end() const { return finish; }
    }
    Hi, now I am having this errors in compile time.
    Please Help Me, I am new with templates!!!
    Regards Reivaj
    Code:
    [C++ Error] hash.h(148): E2015 Ambiguity between 'hash::Hashtable<_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >,_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> > >::hashValue(_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >)' and 'hash::Hashtable<_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >,_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> > >::hashValue(_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >)'
    [C++ Error] hash.h(159): E2015 Ambiguity between 'hash::Hashtable<_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >,_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> > >::hashValue(_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >)' and 'hash::Hashtable<_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >,_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> > >::hashValue(_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >)'
    [C++ Error] hash.h(161): E2015 Ambiguity between '_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >::operator =(const char *)' and '_STL::basic_string<char,_STL::char_traits<char>,_STL::allocator<char> >::operator =(char)'
    // this is my code
    // header file hash.h
    // Define la libreria
    #ifndef _HASH_H
    #define _HASH_H
    
    // Incluye definicion de null
    #include<stddef>
    #include<cstring>
    #include<bitset>
    
    // Tamanyo default y fijo
    #define TABLE_SIZE 10240
    using namespace std;
    
    namespace hash
    {
      template <class Key, class T>
      class Hashtable
      {
        // Miembros publicos
        public:
          // Definicion de tipos
          typedef typename T value_type;
          typedef typename Key key_type;
    
        // Miembros protegidos
        protected:
          // numero de items
          size_t numItems;
    
          // Definicion de la tabla
          value_type table[TABLE_SIZE];
    
        private:
          //Calcula la posicion cuando...
          // La llave es unsigned
          inline unsigned hashValue(unsigned key)
          {
            return (31*key)%TABLE_SIZE;
          }
    
          // La llave es int
          inline unsigned hashValue(int key)
          {
            return (31*key)%TABLE_SIZE;
          }
    
          // La llave es long
          inline unsigned hashValue(unsigned long key)
          {
            return hashValue( reinterpret_cast<unsigned>(key) );
          }
    
          // La llave es long
          inline unsigned hashValue(long key)
          {
            return hashValue( reinterpret_cast<int>(key) );
          }
    
          // La llave es float
          inline unsigned hashValue(float key)
          {
            // Convierte a bits el numero de punto flotante
            unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
            bitset<32> bits(*keyRef);
    
            // convierte los bits a un unsigned long;
            return hashValue( bits.to_ulong() );
          }
    
          // La llave es double
          inline unsigned hashValue(double key)
          {
            // Convierte a bits el numero de punto flotante
            unsigned *keyRef = reinterpret_cast<unsigned*>(&key);
            bitset<32> lsBits(*keyRef); // 32 bits menos significativos
            bitset<32> msBits(*(++keyRef)); // 32 bits mas significativos
    
            // Manipulacion para generar la llave
            lsBits ^= msBits;
            return hashValue( lsBits.to_ulong() );
          }
    
          // La llave es string
          inline unsigned hashValue(string key)
          {
            unsigned h=0;
            size_t len = key.length();
      
            for(unsigned i=0; i<len; i++)
              h = 31*h + key[i];
    
            return h%TABLE_SIZE;
          }
    
          // La llave es char
          inline unsigned hashValue(char key)
          {
            return hashValue( reinterpret_cast<int>(key) );
          }
    
          // La llave es unsigned char
          inline unsigned hashValue(unsigned char key)
          {
            return hashValue( reinterpret_cast<unsigned>(key) );
          }
    
          // La llave es de otro tipo
          inline unsigned hashValue(key_type key)
          {
            return hashValue( reinterpret_cast<unsigned>(&key) );
          }
    
        // Miembros publicos
        public:
    
          // Constructor
          explicit Hashtable()
          {
            // Inicializa el numero de items
            this->numItems = 0;
          }
    
          Hashtable(const Hashtable<Key, T>& t)
          {
            // Inicializa el numero de items
            this->numItems = t.numItems;
    
            // Copia la memoria
            for(int index=0; index<TABLE_SIZE; index++)
              this->table[index] = t.table[index];
          }
    
          // Verifica si la tabla hash contiene la llave
          bool containsKey(key_type key)
          {
            // Ubica la posicion y contenido
            unsigned index = hashValue(key);
            value_type val = table[index];
    
            if(val) return true;
            return false;
          }
    
          // Agrega un elemento a la tabla hash
          value_type put(key_type key, value_type value)
          {
            // Ubica la posicion y contenido
            unsigned index = hashValue(key);
            value_type val = table[index];
            table[index] = value;
    
            return val;
          }
    
          // Obtiene un elemento a la tabla hash
          value_type get(key_type key)
          {
            // Ubica la posicion y contenido
            unsigned index = hashValue(key);
            value_type val = table[index];
            table[index] = NULL;
    
            return val;
          }
      };
    }
    
    #undef TABLE_SIZE
    
    #endif
    
    // test.cpp
    #include<iostream>
    #include<cstring>
    #include"hash.h"
    using namespace std;
    using namespace hash;
    
    Hashtable<string, string> h;
    string guitarristas[10]={ "Steve Vai", "Joe Satriani",
    "Dave Mustaine", "Troy Stetina", "Zakk Wylde",
    "Jimmy Hendrix", "Luca Turilli", "Yngwie Malmsteen",
    "Al DiMeola", "Adrian Smith"};
    
    void main()
    {
      for(int index=0; index<10; index++)
        h.put(guitarristas[index],guitarristas[index]);
    
      for(int index=0; index<10; index++)
        h.get(guitarristas[index]);
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The following line doesn't make sense, since the value is not (necessarily) a pointer:
    Code:
    table[index] = NULL;
    Instead, maybe you could do this to default initialize the object:
    Code:
    table[index] = value_type();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM