To me it seems like it should be so simple, but I just feel like I’m missing something.
I have in my header file a private variable _stoplist When I declared it in the header file the code looks just like this.
private:
std::string _stoplist[];
But when later in my function I decide to access this, it segfaults on anything.
_stoplist[_length];
//cout << _length << prints 104 (its a valid int and everything)
_stoplist[0] = "b";
Crashes in the std::string.assign() code with a segfault. I have a gut feeling that I’m missing something obvious here but I haven’t quite found out what yet.
Thanks in advance!
EDIT: Ok, thanks for all the help. For anyone else who may read this, I would recommend to use one of the answers below since that is the smart way to do it. In my case though since I needed to dynamically allocate it without using vector I just used the following code.
private:
std::string *_stoplist;
and then in my cpp file
_stoplist = new string[_length];
Yeah, turns out that it really was way simple, and I just was over looking that part.
You’re getting an array out of bounds error because _stoplist doesn’t have a size. You should either give it a size, and only access elements within that range, such as:
now you should be able to index _stoplist[0] through _stoplist[99]. However a better solution would probably to use std::vector instead, as it’s a lot safer.
Then you can use its member functions such as resize() to grow it to whatever size you need.