I have some problem with pointer and throw exception. This is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
ifstream out_file("employee.txt");
if (!out_file)
{
cout << "Cannot open file!" << endl;
system("pause");
return;
}
int a = list.size();
cout << a << endl;
for (int i = 0; i < list.size() ; i++)
{
Employee* emp = list.get(i);
emp->XuatThongTin();//error here read access violation
}
get is another function that I define in another cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13
template<class T>
T List<T>::get(int pos) {
int n = sizeof(elements_) / sizeof(elements_[0]);
// if pos < 0 throw out_of_range exception
// if pos >= size của elements
// thorw out_of_range exception
if (pos < 0 || pos >= n)
throw std::out_of_range("error");
// (if pos < size return pos position)
return elements_[index];
}
When I input an information my index still count as the number that I had input. But it still throw exception which is read access violation.
Well, IF the error is in the line that you say it is: emp->XuatThongTin();//error here read access violation
why are you showing a completely different function?
But it still throw exception which is read access violation.
Is 'it' line 8 in the get(...) function? How is elements_ defined?
On line 12 you use index whereas pos is passed as the parameter. What is index? Where is it defined?
#lastchance elements_ is defined as T array in my header.
1 2 3 4 5
template<class T>
class List {
private:
T elements_[1000];
unsignedint index = 0;
Well, IF the error is in the line that you say it is: emp->XuatThongTin();//error here read access violation
why are you showing a completely different function?
That my bad, I thought that was the problem. But I realize that I made a mistake at the last line of my get function. It should return to elements_[pos] not [index] since the param of that function is pos (get(int pos)
#jib
It is from another file for defining a class called Employee
It is from another file for defining a class called Employee
I don't see any memory being allocated in that class?
You have a pointer on line 13 that you don't allocate memory for, big problem. Do you realize that you are creating an "new" Element pointer on that line. Perhaps you don't need the pointer? It looks like List.get() is returning a single Element, not a pointer to an Element.
At first, that loop is originally didn't have that pointer. i just try many case to figure out what I did wrong there. Because you noticed that, I have a question for that since I don't know how to what keyword I should google from it.
when I use new operator. It will be like this. Employee *emp = new Employee
what is the difference with this one
Employee *emp = new Employee()
Even though the second one doesn't have the bracket but the output is also the same. Is it the same process for the output or it's just coincidence? Could you explain for me? It will help me a alot.
Thank you one more time
But I really question the need for that pointer in the first place. Why not just use a normal non-pointer instance since List.get() is returning a normal non-pointer instance.
And remember if you do actually allocate memory for that pointer you must delete what you new.