문제는 기본 클래스와 3 개의 자식 클래스가 있고 모든 3 개의 하위 클래스를 대표하는 모든 요소를 배치 할 수있는 벡터를 만들고 싶습니다. 여기서이, 그것은 컴파일하고 그것뿐만 아니라 실행까지 작동하는 파일C++에서 파일의 벡터로 벡터를 읽는 중
vector<Robot*> robots;
vector<Mac> mac;
vector<Eco> eco;
vector<Pro> pro;
vector<int> charge;
vector<int> deliver;
try {
string s;
ifstream f;
do {
cout << "Add meg a filenevet" << endl;
cin >> s;
f.open(s.c_str());
} while (!f.good());
cout << "adatok beolvasasa..." << endl;
int napok;
if (!(f >> napok)) throw 1;
charge.resize(napok);
deliver.resize(napok);
for (int i = 0; i<napok; i++) {
if (!(f>>charge[i])) throw 1;
if (!(f>>deliver[i])) throw 1;
}
string type, name;
int battery;
int m = 0; int e = 0; int p = 0;
std::string line;
while (std::getline(f, line)) {
stringstream ss(line);
if (ss >> type && ss >> name && ss >> battery) {
if (type=="Mac") {
cout << "mac" << endl;
Mac r = Mac(name,battery);
mac.push_back(r);
robots.push_back(&mac[m]);
m++;
};
if (type=="Eco") {
cout << "eco" << endl;
Eco r = Eco(name,battery);
eco.push_back(r);
robots.push_back(&eco[e]);
e++;
}
if (type=="Pro") {
cout << "pro" << endl;
Pro r = Pro(name,battery);
pro.push_back(r);
robots.push_back(&pro[p]);
p++;
};
}
}
에서 읽고 처리하는 코드의 일부입니다,하지만 난 함수의 예를 robots[i].getBattery();
프로그램 동결에 액세스하려고 할 때. 이 포인터 그냥 아무데도로 가리키는 것 같지만 :(
'robots [i] .getBattery();'를 (를) 호출하는 곳은 어디입니까? 'mac','eco' 및'pro'가 파괴되면 그들의 멤버도 파괴됩니다. 이것은'로봇 '이 잘못된 객체에 대한 포인터를 가지고 있음을 의미합니다. – Oswald
그들은 파괴되지 않습니다 :/ive이 모든 주 기능에 쓰기 (그것은 조금 더 쉽게 만들 것이기를 바래요) 그리고 나는 주 기능에서도 이것을 부릅니다. – Andesz