Home > Software engineering >  Unable to fix the Error code C2065 'Bucket': undeclared identifier
Unable to fix the Error code C2065 'Bucket': undeclared identifier

Time:12-25

I have a problem with my code. I'm getting an error message that says. Severity Code Description Project File Line Suppression State Error C2065 'Bucket': undeclared identifier. But the problem is that I have defined Bucket. Would any of you check what is wrong. The code I'm working on is a code skeleton.

here you get these two important codes.

Ps: Sorry for my English, it's not my mother tongue all the translation is from google translate

    #ifndef BUCKET_H
#define BUCKET_H
#include "Person.h"

typedef Person Value;
typedef int Key;

struct Bucket
{
    Key key;
    Value value;
};

#endif





    #define _CRT_SECURE_NO_WARNINGS // Behovs for vissa funktioner i visual studio
#include "HashTable.h"
#include "Bucket.h"
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>



/* Denna funktion tar en nyckel och returnerar ett hash-index
dvs ett index till arrayen som Šr Hashtabellen */
static int hash(Key key, int tablesize)
{
    return key % tablesize;
}

/*Leta framŒt enligt principen šppen adressering
 Antalet krockar returneras via pekaren col i parameterlistan*/
static int linearProbe(const HashTable* htable, Key key, unsigned int *col)
{
    int index;
    index = hash(key, htable->size);
    int i;
    for (i = 0; i < htable->size; i  ) {
        if (htable->table[index].key == key) {
            return index;
        }
        index = (index   1) % htable->size;
    }
    *col = i;
    return -1;
}

/*Allokera minne fšr hashtabellen*/
HashTable createHashTable(unsigned int size)
{
    HashTable htable;
    htable.table = calloc(size, sizeof(Bucket));  
    htable.size = size;
    return htable;
}

/* Satter in paret {key,data} i Hashtabellen, om en nyckel redan finns ska vardet uppdateras */
/* Returnerar antalet krockar (som rŠknas i linearProbe() )*/
unsigned int insertElement(HashTable* htable, const Key key, const Value value)
{
    unsigned int col = 0;
    int index = linearProbe(htable, key, &col);
    if (index >= 0) {
        htable->table[index].value = value;;
        return col;
    }
    htable->table[index].value = value;
    return col;
}

/* Tar bort datat med nyckel "key" */
void deleteElement(HashTable* htable, const Key key)
{
    unsigned int col = 0;
    int index = linearProbe(htable, key, &col);
    if (index >= 0) {
        htable->table[index].key = 0;
    }
}

/* Returnerar en pekare till vardet som key ar associerat med eller NULL om ingen sadan nyckel finns */
const Value* lookup(const HashTable* htable, const Key key)
{
    unsigned int col = 0;
    int index = linearProbe(htable, key, &col);
    if (index >= 0) {
        return &htable->table[index];
    }
    return NULL;
}



/* Tommer Hashtabellen */
void freeHashTable(HashTable* htable)
{
    free(htable->table);
    free(htable);
}


/* Ger storleken av Hashtabellen */
unsigned int getSize(const HashTable* htable)
{
    return htable->size;
}

/* Denna for att ni enkelt ska kunna visualisera en Hashtabell */
void printHashTable(const HashTable* htable)
{
    for (int i = 0; i < htable->size; i  ) {
        printPerson(&htable->table[i], i);
    }
}

CodePudding user response:

You declare a structure called "struct Bucket" like this:

struct Bucket
{
    Key key;
    Value value;
};

But then you refer to it like this:

htable.table = calloc(size, sizeof(Bucket));

There is no type called "Bucket", only "struct Bucket". If you want to do something like that, better to have:

typedef struct
{
    Key key;
    Value value;
} Bucket;

If you're just typedefing it like this, you can use an anonymous struct, i.e. you don't need to tag the struct as "struct Bucket".

The alternative is to always refer to it as "struct Bucket", e.g.:

htable.table = calloc(size, sizeof(struct Bucket));

I think your code would maybe compile in C , which allows dropping the "struct" when referring to types, but you've tagged this as C.

  •  Tags:  
  • c
  • Related