Throw exception: read access violation

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.

Any recommend that what mistake did i make here?
Last edited on
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?
Where is memory actually allocated for that pointer on line 13 of that first snippet?
#lastchance
elements_ is defined as T array in my header.
1
2
3
4
5
template<class T>
class List {
private:
	T elements_[1000];
	unsigned int 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
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "employee.h"

using namespace std;

Employee::Employee() {
	ma_so_ = "";//ID
	ten_ = "";??Name
	ngay_sinh_ = "";//Birth
	email_ = "";
	dien_thoai_ = "";//Phone
	dia_chi_ = "";//Address
	luong_co_ban_ = 0.0;//Salary

}

Employee::~Employee() {

}

void Employee::NhapThongTin() {
	cin.ignore();
	cout << "Nhap thong tin mot nhan vien moi\n";
	cout << "Ma so:";//ID
	getline(cin, ma_so_);
	cout << "Ho & Ten:";//Name
	getline(cin, ten_);
	cout << "Ngay sinh:";//Birth
	getline(cin, ngay_sinh_);
	cout << "Email:";
	getline(cin, email_);
	cout << "Dien thoai:";//Phone
	getline(cin, dien_thoai_);
	cout << "Dia chi:";//Address
	getline(cin, dia_chi_);
	cout << "Luong co ban:";//Salary
	cin >> luong_co_ban_;
}

void Employee::XuatThongTin() {
	cout << "Ma so: " << ma_so_ << endl;
	cout << "Ho & Ten: " << ten_ << endl;
	cout << "Ngay sinh: " << ngay_sinh_ << endl;
	cout << "Email: " << email_ << endl;
	cout << "Dien thoai: " << dien_thoai_ << endl;
	cout << "Dia chi: " << dia_chi_ << endl;
}

float Employee::TinhLuong() {
	return luong_co_ban_;
}

const std::string& Employee::getDiaChi() const {
	return dia_chi_;
}

void Employee::setDiaChi(const std::string& diaChi) {
	dia_chi_ = diaChi;
}

const std::string& Employee::getDienThoai() const {
	return dien_thoai_;
}

void Employee::setDienThoai(const std::string& dienThoai) {
	dien_thoai_ = dienThoai;
}

const std::string& Employee::getEmail() const {
	return email_;
}

void Employee::setEmail(const std::string& email) {
	email_ = email;
}

float Employee::getLuongCoBan() const {
	return luong_co_ban_;
}

void Employee::setLuongCoBan(float luongCoBan) {
	luong_co_ban_ = luongCoBan;
}

const std::string& Employee::getMaSo() const {
	return ma_so_;
}

void Employee::setMaSo(const std::string& maSo) {
	ma_so_ = maSo;
}

const std::string& Employee::getNgaySinh() const {
	return ngay_sinh_;
}

void Employee::setNgaySinh(const std::string& ngaySinh) {
	ngay_sinh_ = ngaySinh;
}

const std::string& Employee::getTen() const {
	return ten_;
}

void Employee::setTen(const std::string& ten) {
	ten_ = ten;
}


But I got the problem that I return wrong parameter so it always throw exception no matter what input I did.
Last edited on
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.



#jlb

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
Last edited on
I don't see a difference in those two snippets.

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.

#jlb

Thanks for your help! It would be good recommend for me!
Topic archived. No new replies allowed.