Segmentation fault

NEVER MIND
I have a method that simply returns a value from array of ints 'a' from the wanted element e. However, this is making a Segmentation fault error. It is not because of the array, I tested it with a regular int. I believe it has something to do with the const, and it needs to be there. What am I doing wrong?

1
2
int getn(int e) const
{return a[e];}
Last edited on
There is not enough here for me to tell you what the problem is. The const qualifier indicates to me this is a method, correct? And 'a' is a member of the class?

In any case, suppose 'e' is out of bounds of the array. Wouldn't that cause a segfault?
NEVER MIND GOT IT STUPID MISTAKE

private: int a[3];

public:
1
2
3
4
5
void robot::setn(int e, int n)
{a[e]+=n;}  //n is a passed value to add to a[e]
   
int robot::getn(int e) const
{return a[e];}


by calling the getn, i dont get any value back
if a[1] is, in the constructor, initialized to be 0 i get a segmentation fault
Last edited on
Topic archived. No new replies allowed.