Solved

Problems with Hash Tables

Posted on 2000-04-19
51
346 Views
Last Modified: 2010-04-10
Hi i recently bought a book it is Algorithms, Data Structures, and Problem Solving with C++ By Mark Allen Weiss. I am very interested in getting to understand hash tables since I need to see how they insert,delete, and search. For my surprise this book has a webpage of the author in which you can find the classes used in the book. Apart from that, they have little main programs in the book so you can test the classes which are all supposed to working. For my suprise I copied this main exactly like it is in the book and have revised it 10 times and don't find a typing error in it, but when I compile it I get 20 something errors and a warning.
0
Comment
Question by:milalic
  • 36
  • 5
  • 5
  • +2
51 Comments
 

Author Comment

by:milalic
ID: 2732893
This is the main I wrote:

#include<iostream>
#include<string>
#include "Hash.h"
unsigned int Hash(const String & Element, int TableSize);

main()
{
      HashTable<string> H;

      H.Insert("Becky");

      const String & Result2= H.Find("Mark");
      if(H.WasFound())
            cout<<"Found"<<Result2<< ';';
      else
            cout<<"Mark not found; ";
      cout<<'\n';
      return 0;
}


unsigned int Hash(const String & Element, int TableSize)
{
      unsigned int HashVal=0;
      for(int i=0;i<Element.Length();i++)
            HashVal=(HashVal*128+Element[i])% TableSize;
      return HashVal;
}


continues below......
0
 

Author Comment

by:milalic
ID: 2732895
#ifndef __AbsHash
#define __AbsHash

// HashTable abstract class interface
//
// Etype: must have zero-parameter constructor and
//     operator==; implementation may require operator!=;
//     implementation will require either
//     operator= or copy constructor, perhaps both
// unsigned int Hash( const Etype & Element, int TableSize )
//     must be defined
// CONSTRUCTION: with (a) no initializer;
//     copy constructor of HahsTable objects is DISALLOWED
//
// ******************PUBLIC OPERATIONS*********************
//     All of the following are pure virtual functions
// int Insert( Etype X )  --> Insert X
// int Remove( Etype X )  --> Remove X
// Etype Find( Etype X )  --> Return item that matches X
// int WasFound( )        --> Return 1 if last Find succeeded
// int IsFound( Etype X ) --> Return 1 if X would be found
// int IsEmpty( )         --> Return 1 if empty; else return 0
// int IsFull( )          --> Return 1 if full; else return 0
// void MakeEmpty( )      --> Remove all items

template <class Etype>
class AbsHTable
{
  public:
    AbsHTable( ) { }
    virtual ~AbsHTable( ) { }

    virtual int Insert( const Etype & X ) = 0;
    virtual int Remove( const Etype & X ) = 0;
    virtual const Etype & Find( const Etype & X ) = 0;
    virtual int WasFound( ) const = 0;
    virtual int IsFound( const Etype & X ) const = 0;
    virtual int IsEmpty( ) const = 0;
    virtual int IsFull( ) const = 0;
    virtual void MakeEmpty( ) = 0;

  private:
        // Disable copy constructor
    AbsHTable( const AbsHTable & ) { }
};
#endif




// HashTable class interface
//
// Etype: must have zero-parameter and copy constructor,
//     operator= and operator!=
// unsigned int Hash( const Etype & Element, int TableSize )
//     must be defined
// CONSTRUCTION: with (a) no initializer;
// Copy constructon of HashTable objects is DISALLOWED
// Deep copy is supported
//
// ******************PUBLIC OPERATIONS*********************
// int Insert( Etype X )  --> Insert X
// int Remove( Etype X )  --> Remove X
// Etype Find( Etype X )  --> Return item that matches X
// int WasFound( )        --> Return 1 if last Find succeeded
// int IsFound( Etype X ) --> Return 1 if X would be found
// int IsEmpty( )         --> Return 1 if empty; else return 0
// int IsFull( )          --> Return 1 if full; else return 0
// void MakeEmpty( )      --> Remove all items
// ******************ERRORS********************************
// Predefined exception is propogated if new fails

#ifndef __HashTable
#define __HashTable

#include "AbsHash.h"

template <class Etype>
class HashTable : public AbsHTable<Etype>
{
  public:
    enum KindOfEntry { Active, Empty, Deleted };

    HashTable( );
    ~HashTable( ) { delete [ ] Array; }

    const HashTable & operator=( const HashTable & Rhs );

    int Insert( const Etype & X );
    int Remove( const Etype & X );         // Return 1 if found, 0 if not
    const Etype & Find( const Etype & X ); // Return item in table
    int IsFound( const Etype & X ) const;  // Return 1 if found
    int WasFound( ) const;                 // Return 1 if last find ok
    int IsEmpty( ) const;
    int IsFull( ) const { return 0; }      // Return 1 if full
    void MakeEmpty( );
  private:
    struct HashEntry
    {
        Etype Element;         // The item
        KindOfEntry Info;      // Active, empty, or deleted

        HashEntry( ) : Info( HashTable<Etype>::Empty ) { }
        HashEntry( const Etype & E, KindOfEntry i = Empty ) :
            Element( E ), Info( i ) { }
    };

    enum { DefaultSize = 11 };

    int ArraySize;       // The size of this array
    int CurrentSize;     // The number of non-empty items
    int LastFindOK;      // True of last search was successful
    HashEntry *Array;    // The array of elements

        // Some internal routines
    void AllocateArray( );
    unsigned int FindPos( const Etype & X ) const;
};
#endif




Hash.cpp:


#include "hash.h"
#include <iostream.h>
#include <stdlib.h>

// Find next prime > N; assume N >= 5

int
NextPrime( int N )
{
    if( N % 2 == 0 )
        N++;

    for( ; ; N += 2 )
    {
        int i;

        for( i = 3; i * i <= N; i += 2 )
            if( N % i == 0 )
                break;
        if( i * i > N )
            return N;
    }
}

// Allocate the hash table array

template <class Etype>
void
HashTable<Etype>::AllocateArray( )
{
    Array = new HashEntry[ ArraySize ];
}

// HashTable constructor

template <class Etype>
HashTable<Etype>::HashTable( ) : ArraySize ( DefaultSize )
{
    AllocateArray( );
    CurrentSize = 0;
}

// Clear the hash table

template <class Etype>
void
HashTable<Etype>::MakeEmpty( )
{
    CurrentSize = 0;
    for( int i = 0; i < ArraySize; i++ )
        Array[ i ].Info = Empty;
}

// Return item in hash table that matches X
// Success can be tested by WasFound

template <class Etype>
const Etype &
HashTable<Etype>::Find( const Etype & X )
{
    unsigned int CurrentPos = FindPos( X );
    LastFindOK = Array[ CurrentPos ].Info == Active;
    return Array[ CurrentPos ].Element;
}

// Routine to resolve collisions and locate
// cell that must contain X if X is found

template <class Etype>
unsigned int
HashTable<Etype>::FindPos( const Etype & X ) const
{
    unsigned int i = 0;   // The number of failed probes
    unsigned int CurrentPos = Hash( X, ArraySize );

    while( Array[ CurrentPos ].Info != Empty &&
           Array[ CurrentPos ].Element != X )
    {
        CurrentPos += 2 * ++i - 1;    // Compute ith probe
        if( CurrentPos >= ArraySize ) // Implement the mod
            CurrentPos -= ArraySize;
    }

    return CurrentPos;
}

// Test if X is in the hash table

template <class Etype>
int
HashTable<Etype>::IsFound( const Etype & X ) const
{
    unsigned int CurrentPos = FindPos( X );
    return Array[ CurrentPos ].Info == Active;
}


// Return true if last Find operation was successful

template <class Etype>
int
HashTable<Etype>::WasFound( ) const
{
    return LastFindOK;
}

// Remove X from hash table
// Return true if X was removed; false otherwise

template <class Etype>
int
HashTable<Etype>::Remove( const Etype & X )
{
    unsigned int CurrentPos = FindPos( X );

    if( Array[ CurrentPos ].Info != Active )
        return 0;

    Array[ CurrentPos ].Info = Deleted;
    return 1;
}

// Insert X into hash table
// Return false if Xis a duplicate; true otherwise
// Rehash automatically as needed

template <class Etype>
int
HashTable<Etype>::Insert( const Etype & X )
{
    unsigned int CurrentPos = FindPos( X );

        // Don't insert duplicates
    if( Array[ CurrentPos ].Info == Active )
        return 0;

        // Insert X as active
    Array[ CurrentPos ] = HashEntry( X, Active );
    if( ++CurrentSize  < ArraySize / 2 )
        return 1;

        // REHASHING CODE
        // Save old table
    HashEntry *OldArray = Array;
    int OldArraySize = ArraySize;

        // Create a new double-sized, empty table
    CurrentSize = 0;
    ArraySize = NextPrime( 2 * OldArraySize );
    AllocateArray( );

        // Copy table over
    for( int i = 0; i < OldArraySize; i++ )
        if( OldArray[ i ].Info == Active )
            Insert( OldArray[ i ].Element );

        // Recycle OldArray
    delete [ ] OldArray;
    return 1;
}

// Deep copy of the hash table

template <class Etype>
const HashTable<Etype> &
HashTable<Etype>::operator=( const HashTable<Etype> & Rhs )
{
    if( this != &Rhs )
    {
        delete [ ] Array;
        ArraySize = Rhs.ArraySize;
        AllocateArray( );
        for( int i = 0; i < ArraySize; i++ )
            Array[ i ] = Rhs.Array[ i ];
        CurrentSize = Rhs.CurrentSize;
    }

    return *this;
}

// Phoney IsEmpty routine

template <class Etype>
int
HashTable<Etype>::IsEmpty( ) const
{
    return CurrentSize == 0;   // Doesn't work with deletions
}


0
 

Author Comment

by:milalic
ID: 2732904
Hash.cpp compiles without errors. This are the errors given when I compile the main:

Compiling...
Cpp1.cpp
C:\Windows\Desktop\Cpp1.cpp(4) : error C2143: syntax error : missing ',' before '&'
C:\Windows\Desktop\Cpp1.cpp(4) : error C2059: syntax error : '&'
C:\Windows\Desktop\Cpp1.cpp(8) : error C2065: 'string' : undeclared identifier
C:\Windows\Desktop\Cpp1.cpp(8) : error C2955: 'HashTable' : use of class template requires template argument list
        c:\windows\desktop\hash.h(68) : see declaration of 'HashTable'
C:\Windows\Desktop\Cpp1.cpp(8) : error C2133: 'H' : unknown size
C:\Windows\Desktop\Cpp1.cpp(8) : error C2512: 'HashTable' : no appropriate default constructor available
C:\Windows\Desktop\Cpp1.cpp(8) : error C2262: 'H' : cannot be destroyed
C:\Windows\Desktop\Cpp1.cpp(10) : error C2662: 'Insert' : cannot convert 'this' pointer from 'class HashTable' to 'class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
C:\Windows\Desktop\Cpp1.cpp(12) : error C2143: syntax error : missing ';' before '&'
C:\Windows\Desktop\Cpp1.cpp(12) : error C2734: 'String' : const object must be initialized if not extern
C:\Windows\Desktop\Cpp1.cpp(12) : error C2065: 'Result2' : undeclared identifier
C:\Windows\Desktop\Cpp1.cpp(12) : error C2662: 'Find' : cannot convert 'this' pointer from 'class HashTable' to 'class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
C:\Windows\Desktop\Cpp1.cpp(13) : error C2662: 'WasFound' : cannot convert 'this' pointer from 'class HashTable' to 'const class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'const class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
C:\Windows\Desktop\Cpp1.cpp(14) : error C2065: 'cout' : undeclared identifier
C:\Windows\Desktop\Cpp1.cpp(14) : error C2297: '<<' : illegal, right operand has type 'char [6]'
C:\Windows\Desktop\Cpp1.cpp(16) : error C2297: '<<' : illegal, right operand has type 'char [17]'
C:\Windows\Desktop\Cpp1.cpp(17) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
C:\Windows\Desktop\Cpp1.cpp(22) : error C2143: syntax error : missing ',' before '&'
C:\Windows\Desktop\Cpp1.cpp(22) : error C2059: syntax error : '&'
C:\Windows\Desktop\Cpp1.cpp(25) : error C2065: 'Element' : undeclared identifier
C:\Windows\Desktop\Cpp1.cpp(25) : error C2228: left of '.Length' must have class/struct/union type
C:\Windows\Desktop\Cpp1.cpp(26) : error C2109: subscript requires array or pointer type
C:\Windows\Desktop\Cpp1.cpp(26) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp1.obj - 22 error(s), 1 warning(s)


I am using VC++6. Whats happening? Why all those errors? I can't find from where they come. I need help, seeking those errors. Please help!

As much as I went back an read the explanation in the book seeking maybe an error in the main they wrote I wasn't able. They explain in the book like it is correct and fully working. How I am supposed to understand if they explain it with a example that doesn't work? Hope someone can help me correct my errors.
thanx
0
 

Author Comment

by:milalic
ID: 2732906
Adjusted points from 120 to 150
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2733136
Almost all of your compile errors result from the fact that the type "String" is not defined in your program.

Add the following line after the include statements in Cpp1.cpp

    typedef basic_string<char> String;

Also, change the declaration:
    HashTable<string> H;
To:
    HashTable<String> H;
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2733138
Let me know if you still get compile errors and I will help you through them.
0
 

Author Comment

by:milalic
ID: 2733217
It still gives me this bunch of errors when I compile it:Cpp1.cpp
c:\windows\desktop\cpp1.cpp(4) : error C2143: syntax error : missing ';' before '<'
c:\windows\desktop\cpp1.cpp(4) : error C2143: syntax error : missing ';' before '<'
c:\windows\desktop\cpp1.cpp(6) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp1.cpp(6) : error C2059: syntax error : '&'
c:\windows\desktop\cpp1.cpp(10) : error C2065: 'String' : undeclared identifier
c:\windows\desktop\cpp1.cpp(10) : error C2955: 'HashTable' : use of class template requires template argument list
        c:\windows\desktop\hash.h(68) : see declaration of 'HashTable'
c:\windows\desktop\cpp1.cpp(10) : error C2133: 'H' : unknown size
c:\windows\desktop\cpp1.cpp(10) : error C2512: 'HashTable' : no appropriate default constructor available
c:\windows\desktop\cpp1.cpp(10) : error C2262: 'H' : cannot be destroyed
c:\windows\desktop\cpp1.cpp(12) : error C2662: 'Insert' : cannot convert 'this' pointer from 'class HashTable' to 'class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
c:\windows\desktop\cpp1.cpp(14) : error C2143: syntax error : missing ';' before '&'
c:\windows\desktop\cpp1.cpp(14) : error C2734: 'String' : const object must be initialized if not extern
c:\windows\desktop\cpp1.cpp(14) : error C2065: 'Result2' : undeclared identifier
c:\windows\desktop\cpp1.cpp(14) : error C2662: 'Find' : cannot convert 'this' pointer from 'class HashTable' to 'class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
c:\windows\desktop\cpp1.cpp(15) : error C2662: 'WasFound' : cannot convert 'this' pointer from 'class HashTable' to 'const class HashTable<Etype> &'
        Reason: cannot convert from 'class HashTable' to 'const class HashTable<Etype>'
        Conversion requires a second user-defined-conversion operator or constructor
c:\windows\desktop\cpp1.cpp(16) : error C2065: 'cout' : undeclared identifier
c:\windows\desktop\cpp1.cpp(16) : error C2297: '<<' : illegal, right operand has type 'char [6]'
c:\windows\desktop\cpp1.cpp(18) : error C2297: '<<' : illegal, right operand has type 'char [17]'
c:\windows\desktop\cpp1.cpp(19) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\windows\desktop\cpp1.cpp(24) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp1.cpp(24) : error C2059: syntax error : '&'
c:\windows\desktop\cpp1.cpp(27) : error C2065: 'Element' : undeclared identifier
c:\windows\desktop\cpp1.cpp(27) : error C2228: left of '.Length' must have class/struct/union type
c:\windows\desktop\cpp1.cpp(28) : error C2109: subscript requires array or pointer type
c:\windows\desktop\cpp1.cpp(28) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp1.obj - 24 error(s), 1 warning(s)


I made the changes you told me. Here they are:

#include<iostream>
#include<string>
#include "Hash.h"
typedef basic_string<char> String;

unsigned int Hash(const String & Element, int TableSize);

main()
{
  HashTable <String> H;

  H.Insert("Becky");

  const String &Result2= H.Find("Mark");
  if(H.WasFound())
   
   cout<<"Found"<<Result2<< ';';

  else
   cout<<"Mark not found; ";
   cout<<'\n';
  return 0;
}


unsigned int Hash(const String &Element, int TableSize)
{
  unsigned int HashVal=0;
  for(int i=0;i<Element.Length();i++)
            
   HashVal=(HashVal*128+Elemen[i])%     TableSize;      
return HashVal;
}

0
 

Author Comment

by:milalic
ID: 2733225
I really appreciate you helping. Hope ya can help me get this working as soon as possible. I have no idea what is worng.

=(
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2733342
Do you need to put strings into the hash table, or could you use anything?

Could you use CString?

The truth is that I have never used "string" before and so don't really know how to get it compiling.

If you do need to use strings, and you can't use CString, I will write you some code that will definitely work.
0
 
LVL 5

Expert Comment

by:proskig
ID: 2733419
After all includes try to add the statement.

using namespace std;

But do it in the original program.
0
 
LVL 5

Expert Comment

by:proskig
ID: 2733421
Can you also post URL with source code?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2733882
Looks like the namespace thing was missing. Also check the compilers documentation about the use of the C++ library string class. There have been major changes the last years and not all compiler vendors stayed completely up to date.
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2734298
I'll withdraw my answer since it obviously wasn't correct.

Hopefully proskig's comment will fix your program. If not, I will wait for your answer to my last comment.

Mike.
0
 

Author Comment

by:milalic
ID: 2735029
I added what proskig said and i still get errors. This is the main:


#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const String & Element, int TableSize);

main()
{
HashTable<string> H;

H.Insert("Becky");

const String & Result2= H.Find("Mark");
if(H.WasFound())
cout<<"Found"<<Result2<< ';';
else
cout<<"Mark not found; ";
cout<<'\n';
return 0;
}

This are the errors:

Cpp2.cpp
c:\windows\desktop\cpp2.cpp(24) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp2.cpp(24) : error C2059: syntax error : '&'
c:\windows\desktop\cpp2.cpp(27) : error C2065: 'Element' : undeclared identifier
c:\windows\desktop\cpp2.cpp(27) : error C2228: left of '.Length' must have class/struct/union type
c:\windows\desktop\cpp2.cpp(28) : error C2109: subscript requires array or pointer type
c:\windows\desktop\cpp2.cpp(28) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp2.obj - 6 error(s), 0 warning(s)



unsigned int Hash(const String & Element, int TableSize)
{
unsigned int HashVal=0;
for(int i=0;i<Element.Length();i++)
HashVal=(HashVal*128+Element[i])% TableSize;
return HashVal;
}


0
 

Author Comment

by:milalic
ID: 2735043
I added what proskig said and i still get errors. This is the main:


#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const String & Element, int TableSize);

main()
{
HashTable<string> H;

H.Insert("Becky");

const String & Result2= H.Find("Mark");
if(H.WasFound())
cout<<"Found"<<Result2<< ';';
else
cout<<"Mark not found; ";
cout<<'\n';
return 0;
}

This are the errors:

Cpp2.cpp
c:\windows\desktop\cpp2.cpp(24) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp2.cpp(24) : error C2059: syntax error : '&'
c:\windows\desktop\cpp2.cpp(27) : error C2065: 'Element' : undeclared identifier
c:\windows\desktop\cpp2.cpp(27) : error C2228: left of '.Length' must have class/struct/union type
c:\windows\desktop\cpp2.cpp(28) : error C2109: subscript requires array or pointer type
c:\windows\desktop\cpp2.cpp(28) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp2.obj - 6 error(s), 0 warning(s)



unsigned int Hash(const String & Element, int TableSize)
{
unsigned int HashVal=0;
for(int i=0;i<Element.Length();i++)
HashVal=(HashVal*128+Element[i])% TableSize;
return HashVal;
}


0
 

Author Comment

by:milalic
ID: 2735081
Repsoting last comment because it is confusing, having troubles posting:

I added what proskig said and i still get errors. This is the main:


#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const String & Element, int TableSize);

main()
{
HashTable<string> H;

H.Insert("Becky");

const String & Result2= H.Find("Mark");
if(H.WasFound())
cout<<"Found"<<Result2<< ';';
else
cout<<"Mark not found; ";
cout<<'\n';
return 0;
}

unsigned int Hash(const String & Element, int TableSize)
{
unsigned int HashVal=0;
for(int i=0;i<Element.Length();i++)
HashVal=(HashVal*128+Element[i])% TableSize;
return HashVal;
}


This are the errors:

Cpp2.cpp
c:\windows\desktop\cpp2.cpp(24) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp2.cpp(24) : error C2059: syntax error : '&'
c:\windows\desktop\cpp2.cpp(27) : error C2065: 'Element' : undeclared identifier
c:\windows\desktop\cpp2.cpp(27) : error C2228: left of '.Length' must have class/struct/union type
c:\windows\desktop\cpp2.cpp(28) : error C2109: subscript requires array or pointer type
c:\windows\desktop\cpp2.cpp(28) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp2.obj - 6 error(s), 0 warning(s)


 



0
 

Author Comment

by:milalic
ID: 2735094
Repsoting last comment because it is confusing, having troubles posting:

I added what proskig said and i still get errors. This is the main:


#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const String & Element, int TableSize);

main()
{
HashTable<string> H;

H.Insert("Becky");

const String & Result2= H.Find("Mark");
if(H.WasFound())
cout<<"Found"<<Result2<< ';';
else
cout<<"Mark not found; ";
cout<<'\n';
return 0;
}

unsigned int Hash(const String & Element, int TableSize)
{
unsigned int HashVal=0;
for(int i=0;i<Element.Length();i++)
HashVal=(HashVal*128+Element[i])% TableSize;
return HashVal;
}


This are the errors:

Cpp2.cpp
c:\windows\desktop\cpp2.cpp(24) : error C2143: syntax error : missing ',' before '&'
c:\windows\desktop\cpp2.cpp(24) : error C2059: syntax error : '&'
c:\windows\desktop\cpp2.cpp(27) : error C2065: 'Element' : undeclared identifier
c:\windows\desktop\cpp2.cpp(27) : error C2228: left of '.Length' must have class/struct/union type
c:\windows\desktop\cpp2.cpp(28) : error C2109: subscript requires array or pointer type
c:\windows\desktop\cpp2.cpp(28) : error C2065: 'TableSize' : undeclared identifier
Error executing cl.exe.

Cpp2.obj - 6 error(s), 0 warning(s)


 



0
 

Author Comment

by:milalic
ID: 2735129
proskig....you want me to post all the code I posted here in a webpage?


mnewton...any solution, code, etc that you can provide to solve my problem is appreciated

I am using VC++6.0. I also have DJGPP. Both give me the same errors.

0
 

Author Comment

by:milalic
ID: 2735140
proskig....you want me to post all the code I posted here in a webpage?


mnewton...any solution, code, etc that you can provide to solve my problem is appreciated

I am using VC++6.0. I also have DJGPP. Both give me the same errors.

0
 

Author Comment

by:milalic
ID: 2735146
proskig....you want me to post all the code I posted here in a webpage?


mnewton...any solution, code, etc that you can provide to solve my problem is appreciated

I am using VC++6.0. I also have DJGPP. Both give me the same errors.

0
 

Author Comment

by:milalic
ID: 2735148
proskig....you want me to post all the code I posted here in a webpage?


mnewton...any solution, code, etc that you can provide to solve my problem is appreciated

I am using VC++6.0. I also have DJGPP. Both give me the same errors.

0
 
LVL 5

Expert Comment

by:proskig
ID: 2735234
Make String starting with small letter (string). I mentioned it already:
(But do it in the original program.) If you still have a problem, please post an URL so we can download a code
0
 

Author Comment

by:milalic
ID: 2735593
I am doing it in the original program. I am setting a webpage with the files. So you can Download them and anyone who wants to help download them.
0
 

Author Comment

by:milalic
ID: 2735643
It doesn't link with the changes you suggested.
here is the main:

#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const string & Element, int TableSize);

main()
{
  HashTable <string> H;

  H.Insert("Becky");

  const string &Result2= H.Find("Mark");
  if(H.WasFound())
    cout<<"Found"<<Result2<< ';';
  else
    cout<<"Mark not found; ";
  cout<<'\n';
  return 0;
}


unsigned int Hash(const string &Element, int TableSize)
{
  unsigned int HashVal=0;
  for(int i=0;i<Element.length();i++)
    HashVal=(HashVal*128+Element[i])% TableSize;
  return HashVal;
}

0
 

Author Comment

by:milalic
ID: 2735659
It doesn't link with the changes you suggested.
here is the main:

#include<iostream>
#include<string>
#include "Hash.h"
using namespace std;

unsigned int Hash(const string & Element, int TableSize);

main()
{
  HashTable <string> H;

  H.Insert("Becky");

  const string &Result2= H.Find("Mark");
  if(H.WasFound())
    cout<<"Found"<<Result2<< ';';
  else
    cout<<"Mark not found; ";
  cout<<'\n';
  return 0;
}


unsigned int Hash(const string &Element, int TableSize)
{
  unsigned int HashVal=0;
  for(int i=0;i<Element.length();i++)
    HashVal=(HashVal*128+Element[i])% TableSize;
  return HashVal;
}

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:milalic
ID: 2735700
Here is what it says:

Linking...
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::WasFound(void)const " (?WasFound@?$HashTable@V?$basic_string@DU
?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UBEHXZ)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,cla
ss std::allocator<char> > >::Find(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?Find@?$HashTable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UAEABV?$basic_string@DU?$char_trai
ts@D@std@@V?$allocator@D@2@@std@@ABV23@@Z)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::Insert(class std::basic_string<char,struct std::char_traits<cha
r>,class std::allocator<char> > const &)" (?Insert@?$HashTable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UAEHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Cpp2.obj : error LNK2001: unresolved external symbol "public: __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::HashTable<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > >(void)" (??0?$HashTable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::MakeEmpty(void)" (?MakeEmpty@?$HashTable@V?$basic_string@DU?$c
har_traits@D@std@@V?$allocator@D@2@@std@@@@UAEXXZ)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::IsEmpty(void)const " (?IsEmpty@?$HashTable@V?$basic_string@DU?$
char_traits@D@std@@V?$allocator@D@2@@std@@@@UBEHXZ)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::IsFound(class std::basic_string<char,struct std::char_traits<ch
ar>,class std::allocator<char> > const &)const " (?IsFound@?$HashTable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UBEHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Cpp2.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall HashTable<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::Remove(class std::basic_string<char,struct std::char_traits<cha
r>,class std::allocator<char> > const &)" (?Remove@?$HashTable@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UAEHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
Debug/Cpp2.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.

Cpp2.exe - 9 error(s), 0 warning(s)



0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2735730
Doesn't link, what is the message, ie, what external reference is not found?

Don't use the refresh button, and don't submit twice, if you percieve problems. After hitting submit the comment is immediately send to EE. Next the browser will attempt to load the updated question. Sometimes this fails, but the comment is still added to the question. If you have problems often make a copy of the comment (select everything you typed and copy it to the clipboard). Submit. If the connection fails, open a new brwoser window and navigate to your question. Check if your comment has been added.
0
 
LVL 5

Expert Comment

by:proskig
ID: 2735734
Once again: can you post an URL with full source code?
0
 

Author Comment

by:milalic
ID: 2735797
here is the URL with all the files I am using if you wish to download. I'll appreciate your help in solving my problem.

thanx

http://www.geocities.com/q_low_designs/help.html
0
 

Author Comment

by:milalic
ID: 2735816
here is the URL with all the files I am using if you wish to download. I'll appreciate your help in solving my problem.

thanx

http://www.geocities.com/q_low_designs/help.html
0
 

Author Comment

by:milalic
ID: 2735823
here is the URL with all the files I am using if you wish to download. I'll appreciate your help in solving my problem.

thanx

http://www.geocities.com/q_low_designs/help.html
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2735824
Off course, I assume you placed the implementations (code) of the HashTable classes neatly in a seperate .cpp file? Join the club most of did and were surprised like you.
To cut a long story short, you need to __include__ the implementation file, just as the header file! That is because the compiler only instantiates a template when it is used, and it can only instantiate what it 'sees', that is the code in the current (.cpp) file it is being compiled and all the files that are included. So, include the Hash.cpp file as well....
0
 

Author Comment

by:milalic
ID: 2735871
Including that file gives me errors when compiled. Here is the fixed main:
#include<iostream>
#include<string>
#include "Hash.h"
#include "Hash.cpp"
using namespace std;

unsigned int Hash(const string & Element, int TableSize);

main()
{
  HashTable <string> H;

  H.Insert("Becky");

  const string &Result2= H.Find("Mark");
  if(H.WasFound())
    cout<<"Found"<<Result2<< ';';
      
  else
  {
    cout<<"Mark not found; ";
    cout<<'\n';
  }
return 0;
}


unsigned int Hash(const string &Element, int TableSize)
{
      unsigned int HashVal=0;
      for(int i=0;i<Element.length();i++)
            HashVal=(HashVal*128+Element[i])% TableSize;
      return HashVal;
}

Here are the errors when I compile it:

Compiling...
Cpp2.cpp
C:\Windows\Desktop\Cpp2.cpp(17) : error C2872: 'cout' : ambiguous symbol
C:\Windows\Desktop\Cpp2.cpp(17) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conv
ersion)
C:\Windows\Desktop\Cpp2.cpp(20) : error C2872: 'cout' : ambiguous symbol
C:\Windows\Desktop\Cpp2.cpp(21) : error C2872: 'cout' : ambiguous symbol
Error executing cl.exe.

Cpp2.obj - 4 error(s), 0 warning(s)


0
 

Author Comment

by:milalic
ID: 2735886
Sorry for the postings of messages more than one time.I have hitted the submit button just once and I tried what KangaRoo advice for this and still posted my message more than once. Hope it doesn't keep like that
0
 

Author Comment

by:milalic
ID: 2735954
Adjusted points from 150 to 200
0
 

Author Comment

by:milalic
ID: 2735955
I need to understand this really good,  because the next step in what I am planning to do depends on getting this class to work with strings. I appreciate the help all of you are giving and hope that between all of you and me can arrive to a solution to my problem.
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 210 total points
ID: 2736024
That is odd, but it did get rid of the undefined symbols (the linker did not get into  play yet ;)
This did not occur when Hash.cpp is not included??
0
 
LVL 1

Expert Comment

by:abk102299
ID: 2736125
Delete the following lines:

#include <iostream.h>
#include <stdlib.h>

from the "Hash.cpp". And I think you'd better to get some c++ minimum before going to Algorithms, Data Structures, etc.

0
 

Author Comment

by:milalic
ID: 2736299
abk....And I think you'd better to get some c++ minimum before going to Algorithms, Data Structures, etc.


Well I did, The book was how to program C++ by deitel & deitel



0
 

Author Comment

by:milalic
ID: 2736317
abk...I took those lines out and it seems to work. I'll go catch something to eat and be back with the answers and some other questions I have for all of you that have been helping.
thanx
0
 

Author Comment

by:milalic
ID: 2737813
Okay the program works guys. Thanx
0
 

Author Comment

by:milalic
ID: 2737867
I got a question. What about if I want the user to enter the string and then I hash to the table the string he or she enters?Would getline or cin.getline command work?
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2738210
How about the standard stream extraction:

sting to_read;
cin >> to_read;
0
 

Author Comment

by:milalic
ID: 2738383
I did so. If I enter a name that only has one word, it works fine. But if I enter a name that has more than one work like The Mirror, if only says it found The. I want it to be able to find whole string that I write. If i write The Mirror, it should tell me if fount The Mirror, not that it found The.

Here is my code:

main()
{
  HashTable <string> H;
  string name;
  cout<<"Enter Name: ";
  cin>>name;

  H.Insert(name);
      


  const string &Result2= H.Find(name);
  if(H.WasFound())
  {
      cout<<"Found "<< Result2;
            cout<< '\n';
  }
  else
  {
          cout<<"not found; ";
          cout<<'\n';
  }
      
  return 0;
}


unsigned int Hash(const string &Element, int TableSize)
{
  unsigned int HashVal=0;
  for(int i=0;i<Element.length();i++)
     HashVal=(HashVal*128+Element[i])% TableSize;
      return HashVal;
}

0
 

Author Comment

by:milalic
ID: 2738886
Adjusted points from 200 to 210
0
 

Author Comment

by:milalic
ID: 2738897
I still can make the program output the two words. It truncates it if it is more than one word.
0
 

Author Comment

by:milalic
ID: 2739133
I also would like to give points to some of you. Cuz some have helped in some stuff and others in other.
0
 

Author Comment

by:milalic
ID: 2740599
Help!!!!
0
 
LVL 5

Expert Comment

by:proskig
ID: 2746439
As STL documentation says you read from cin until whitespace. If you want to read the whole line, then use getline().
0
 

Author Comment

by:milalic
ID: 2748631
so use getline
0
 

Author Comment

by:milalic
ID: 2749377
souse geltline?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now