c++ 코드 0xC0000005: 0x0000000000000000 위치를 읽는 동안 액세스 위반
조회수 354회
include
include
include
using namespace std;
class Point { public:
Point(std::string pname = "", int px = "", int py = "")
{
setName(pname); setX(px); setY(py);
}
std::string getName() { return name; }
int getX() { return x; }
int getY() { return y; }
void setName(std::string pname) { name = pname; }
void setX(int px) { x = px; }
void setY(int py) { y = py; }
private:
std::string name;
int x;
int y;
};
int main() { int a ; int counter = 0; cout << "number of points" << endl; cin >> a; vector v(a); while (counter < a) { Point p1;
string tmp;
int tmp_x;
int tmp_y;
cout << "name of point" << endl;
cin >> tmp;
p1.setName(tmp);
cout << "position of point" << endl;
cin >> tmp_x >> tmp_y;
p1.setX(tmp_x);
p1.setY(tmp_y);
v.push_back(p1);
cout << p1.getName() << p1.getX() << p1.getY() << endl;
counter++;
}
int total_x{}, total_y{};
for (auto& point : v) {
total_x += point.getX();
total_y += point.getY();
}
std::cout << "Average X: " << total_x / v.size()
<< "Average Y: " << total_y / v.size() << std::endl;
cout << "Center of 3 points:" << setprecision(1) << total_x << total_y << endl;
return 0;
}
위 코드를 실행하니 "0xC0000005: 0x0000000000000000 위치를 읽는 동안 액세스 위반이 발생했습니다." 라고 나옵니다. 이 오류는 어떻게 해결해야 하나요? 구글링 해보니 " 프로세스가 접근할 권한이 없는 메모리 영역에 접근하고자 했을 때 발생"한다고 나와서 확인해보았으나 짐작되는 부분이 없어서 질문 드립니다.
댓글 입력