I have defined a vector like this in the header file
class entry
{
public:
int key;
int next;
};
std::vector<entry *> TB;
in the cpp file, I wrote:
int s1, val;
s1 = 10; val = 2;
gh = (TB.size() % s1);
However when I want to write something to it, I get segmentation fault
TB[gh]->key = val;
What is the problem with the assignment?
The vector has no elements. Use
push_back()orinsert()to add elements to the vector:When destroying the vector
TByou must iterate over the elements anddeleteeach individually (or use a smart pointer as the element type, such asboost::shared_ptr<entry>orstd::unique_ptr<entry>).You could provide a constructor(s) for
entryto make the addition of anentrytoTBmore concise: