Ir ao conteúdo
  • Cadastre-se

Tabela hash erro: invalid conversion from `char' to `const char*'


Pereira16

Posts recomendados

Tenho um compilador para fazer.
Na net alguém disponibilizou uma tabela hash feita com string, porém preciso fazer com char poís com string não deu certo. Fiz umas adaptações, esta dando esse erro:
main.cpp invalid conversion from `char' to `const char*'  quando chama a função ObjetoHash.BuscaPalavra(palavra);  para pesquisar se o que foi digitado esta na tabela.
 
Programa:
 
 
main.cpp
 

#include <iostream>
#include "hash.h"
#include <string>
#include <cstdlib>
 
using namespace std;
int i = 0, j = 0;
 
int main(int argc, char *argv[])
{
   hash ObjetoHash;
 
   ObjetoHash.Inserir("auto");
   ObjetoHash.Inserir("asm");
   ObjetoHash.Inserir("bool");
   ObjetoHash.Inserir("break");
   ObjetoHash.Inserir("case");
   ObjetoHash.Inserir("catch");
   ObjetoHash.Inserir("char");
   ObjetoHash.Inserir("class");
   ObjetoHash.Inserir("const");
   ObjetoHash.Inserir("const_cast");
   ObjetoHash.Inserir("continue");
   ObjetoHash.Inserir("default");
   ObjetoHash.Inserir("delete");
   ObjetoHash.Inserir("do");
   ObjetoHash.Inserir("double");
   ObjetoHash.Inserir("dynamic_cast");
   ObjetoHash.Inserir("else");
   ObjetoHash.Inserir("enum");
   ObjetoHash.Inserir("explicit");
   ObjetoHash.Inserir("extern");
   ObjetoHash.Inserir("false");
   ObjetoHash.Inserir("float");
   ObjetoHash.Inserir("for");
   ObjetoHash.Inserir("friend");
   ObjetoHash.Inserir("goto");
   ObjetoHash.Inserir("if");
   ObjetoHash.Inserir("inline");
   ObjetoHash.Inserir("int");
   ObjetoHash.Inserir("long");
   ObjetoHash.Inserir("mutable");
   ObjetoHash.Inserir("namespace");
   ObjetoHash.Inserir("new");
   ObjetoHash.Inserir("operator");
   ObjetoHash.Inserir("private");
   ObjetoHash.Inserir("protected");
   ObjetoHash.Inserir("public");
   ObjetoHash.Inserir("register");
   ObjetoHash.Inserir("reinterpret_cast");
   ObjetoHash.Inserir("return");
   ObjetoHash.Inserir("short");
   ObjetoHash.Inserir("signed");
   ObjetoHash.Inserir("sizeof");
   ObjetoHash.Inserir("static");
   ObjetoHash.Inserir("static_cast");
   ObjetoHash.Inserir("struct");
   ObjetoHash.Inserir("switch");
   ObjetoHash.Inserir("template");
   ObjetoHash.Inserir("this");
   ObjetoHash.Inserir("throw");
   ObjetoHash.Inserir("true");
   ObjetoHash.Inserir("try");
   ObjetoHash.Inserir("typedef");
   ObjetoHash.Inserir("typeid");
   ObjetoHash.Inserir("typename");
   ObjetoHash.Inserir("union");
   ObjetoHash.Inserir("unsigned");
   ObjetoHash.Inserir("using");
   ObjetoHash.Inserir("virtual");
   ObjetoHash.Inserir("void");
   ObjetoHash.Inserir("volatile");
   ObjetoHash.Inserir("wchar_t");
   ObjetoHash.Inserir("while");
 
   int opcao = 1;
   char palavra[50];
   char lexeme[50];
 
           cout << "Digite: ";
           gets(palavra);
   
   while (palavra != '!'){
 
       
 
       if (opcao == 1){
          
          if(palavra == '+')
          {
                   lexeme[j] = palavra;
                   i++;
                   j++;
                   opcao = 3;       
          }
          else opcao = 2;
          
          }
           
       if (opcao == 2){
            
       if((palavra== ' ')||(palavra=='\n'))
       i++;
           
         else if(opcao == 2){
 
               if(palavra != '!'){
                   // erro esta aqui
                  ObjetoHash.BuscaPalavra(palavra);
                   lexeme[j] = palavra;
                   i++;
                   j++;
                   opcao = 4;     
               }
               
               else opcao = 3;
           }
           }
       
       if (opcao == 3){
                   cout<<"\n Constante";
                   cout<<"\n Palavra = " <<palavra<<"\n\n";
                   system("pause"); 
                   j=0;
                   opcao = 1;
                   
          }
           
       if (opcao == 4){
                   cout<<"\n Tabela";
                   cout<<"\n lexeme = " <<lexeme<<"\n\n";
                   system("pause");
                   j=0;
                   opcao = 1;
                   
       }
 
       
   }
}
 

 

 
hash.cpp
 

#include <iostream>
#include <iostream>
#include "hash.h"
#include <string>
#include <cstdlib>
 
using namespace std;
 
hash::hash()
{
   for(int i = 0; i < TamanhoTabela; i++){
       TabelaHash = new item;
       TabelaHash->reservada = "vazio";
       TabelaHash->prox = NULL;
   }
}
 
void hash::Inserir(string reservada)
{
   int indice = Hash(reservada);
 
   if(TabelaHash[indice]->reservada == "vazio")
   {
       TabelaHash[indice]->reservada = reservada;
   }
   else{
       item* ptr = TabelaHash[indice];
       item* n = new item;
       n->reservada = reservada;
       n->prox = NULL;
       while(ptr->prox != NULL){//faz o ponteiro percorrer até o final da table
           ptr = ptr->prox;
       }
       ptr->prox = n;
   }
}
 
int hash::NumeroDeItensNaLista(int indice){
   int contagem = 0;
 
   if(TabelaHash[indice]->reservada=="vazio"){
       return contagem;
   }
   else{
       contagem++;
       item* ptr = TabelaHash[indice];
       while(ptr->prox != NULL){
           contagem++;
           ptr = ptr->prox;
       }
   }
   return contagem;
}
 
 
void hash::ImprimirTabela(){
   int numero;
 
   for(int i = 0;i < TamanhoTabela; i++){
       numero = NumeroDeItensNaLista(i);
       cout<<"-------------------\n";
       cout<<"indice = "<<i<<endl;
       cout<<TabelaHash->reservada <<endl;
       cout<<"# de itens na neste indice: "<<numero<<endl;
       cout<<"-------------------\n";
   }
}
 
void hash::ImprimirItensNoIndice(int indice){
   item* ptr = TabelaHash[indice];
 
   if(ptr->reservada == "vazio"){
       cout<<"indice = "<<indice<<" esta vazio"<<endl;
   }
   else{
       cout<<"indice = "<<indice<<" tem os seguintes itens: "<<endl;
       while(ptr!=NULL){
           cout<<"-------------------\n";
           cout<<ptr->reservada <<endl;
           cout<<"-------------------\n";
           ptr = ptr->prox;
       }
   }
}
 
int hash::Hash(string chave){
   int hash = 0;
   int indice;
 
 
   for(int i = 0; i<chave.length();i++)
   {
       hash = hash + (int)chave;
   }
 
   indice = hash % TamanhoTabela;
 
   return indice;
 
}
 
void hash::BuscaPalavra(string reservada){
   int indice = Hash(reservada);
   bool EncontrarPalavra = false;
   string palavra;
 
   item* ptr = TabelaHash[indice];
   while(ptr != NULL){        //percorrer a tabela
       if(ptr->reservada == reservada){
           EncontrarPalavra = true;
           palavra = ptr->reservada;
       }
       ptr=ptr->prox;
   }
   if(EncontrarPalavra == true){
       cout<<"A palavra esta na tabela"<<endl;
       system("pause");
   }
 
}
 
hash::~hash()
{
  item* ptr;
  for(int i=0;i<TamanhoTabela;i++)
  {
     while(TabelaHash != NULL)
     {
        ptr = TabelaHash;
        TabelaHash = TabelaHash->prox;
        delete ptr;
     }
  }
}
 

 

 
hash.h
 
 

#ifndef HASH_H
#define HASH_H
 
#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
class hash
{
private:
   static const int TamanhoTabela = 50; //define o tamanho da tabela dentro da classe
 
   struct item{
       string reservada;
       item *prox;
   };
 
   item* TabelaHash[TamanhoTabela]; //ponteiro com o tamanho da tabela
 
public:
   int Hash(string chave);
   void Inserir(string reservada);
   int NumeroDeItensNaLista(int indice);
   void ImprimirTabela();
   void ImprimirItensNoIndice(int indice);
   void BuscaPalavra(string reservada);
 
   hash();
   ~hash();
 
};
 
#endif // HASH_H
 

 

 
Alguém poderia me ajudar ?
Ficarei muito agradecido.

Hash.rar

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...