Error: no match for operator=

Dec 17, 2020 at 7:33pm
I want to initialize a hash table but I keep getting an error. How do i properly access elements from HashTables?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//HashTable struct
struct HashTable {
	Condition elements [MAX_ELEMENTS];
	int sizeTable;
};

//Condition struct
struct Condition {
	string conditionName;
	int priority;
};

//Hash Table initialization
  HashTable * initHT (int sizeTable) {
	
	// write code to create a new hash table with the given size, initialize the hash table, and return the address of the hash table created.
        HashTable *ht= new HashTable; 

        int i;
       	for (i=0; i<sizeTable; i++) {
         	ht->elements[i]=NULL;     //error: no match for operator=  
     	}
       	ht->sizeTable = sizeTable;
return ht;
}
Dec 17, 2020 at 7:36pm
NULL is used with pointers.
elements contains Condition objects, not pointers.
Dec 17, 2020 at 7:37pm
HashTable::elements is an array of Condition objects.

You tell us, what does it mean to assign NULL to an object of type Condition?
If you just want some sane initial values, you could just do

1
2
3
for (int i = 0; i < sizeTable; i++) {
    ht->elements[i].priority = 0;
}

(std::strings by default are already empty)

Alternatively, you could change your struct to be:
1
2
3
4
struct Condition {
	string conditionName;
	int priority {};
};
which will automatically zero-initialize the int.
Last edited on Dec 17, 2020 at 7:42pm
Dec 17, 2020 at 7:44pm
Oh ok, I thought you had to outright state you want the strings empty. Thanks for the info
Dec 18, 2020 at 10:44am
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Condition struct
struct Condition {
	string conditionName;
	int priority {};
};

//HashTable struct
struct HashTable {
	Condition elements[MAX_ELEMENTS] {};
	int sizeTable {};

	HashTable(int s) : sizeTable(s) {}
};


//Hash Table initialization
HashTable* initHT(int sizeTable)
{
	return new HashTable {sizeTable};
}


to default init the hashtable to a specified size.
Topic archived. No new replies allowed.