간단한 리소스 수집 게임을 시뮬레이트하는 프로그램 로봇이지도에서 자원을 수집하고 각각의 동작을 무작위로 이동합니다. 내 문제는 로봇 "deriguebot"의 클래스가 생성 된 클래스 맵에서 벡터에 액세스하려고한다는 것입니다. 이 프로그램은 내가 객체의 벡터를 MAIN.CPP벡터 객체가 벡터 요소에 액세스하는 방법
, 여러 파일, header.h가, header.cpp에 쓰여진 "로봇"내 header.h가 파일의 예 입력된다
class Map{
private:
char World[20][20]; // The size of Map is 20 x 20
vector<Robots*>RobotsVector;
public:
Map();
vector<Robots*>*getRobotsVector();
}
// I access the vector with getRobotsVector() which belongs to Map class but returns
// Robot objects. Done this so i can access Robot objects within the Map class.
class Robots
{
private:
//some variables
public:
//some functions
virtual void movement()=0; // this is the function that handles the robots moves
virtual void action()=0; // this is the function that handles the robots actions
}
class RescueBot:public Robots{
void movement();
void action();
//some unique RescueBot stuff here
}
을
이것은 header.cpp 파일에서입니다 :
#include "header.h"
vector<Robots*>*Map::getRobotsVector(){return &RobotsVector;}
//example of object creation and pushing into vector
void Map::creation(){
for (int x=0;x<4;x++){
getRobotsVector()->push_back(new RescueBot);
}
}
void RescueBot::action(){
//do stuff
for(int i=0;i<Map::getRobotsVector()->size();i++){
//Here is the problem. I cant get inside the for loop
Map::getRobotsVector()->at(i)->setDamaged(false); //Changes a flag in other objects
}
}
나는지도의 로봇 클래스 derivered 클래스를 만드는 노력했다. 그 후에 실제 벡터에 객체가있는 동안 RescueBot :: action의 벡터 i 액세스는 비어 있습니다. 만약 내가 그것을 만들지 않는다면 컴파일이 안된다.
RescueBot :: action() 내에서 벡터에 액세스하려면 어떻게해야합니까 ??
그것은, 또는에 추가하는 각 개체의 벡터에 대한 포인터를 설정할 수 없습니다 :
이것은 액션 기능은 다음과 같을 것이다 것입니다 그 벡터. 일반적으로 어느 쪽도 특히 좋은 아이디어는 아닙니다. – dasblinkenlight
잘못된 생각. RescueBot이 Map을 수정해야하는 경우 생성자 또는 동작 명령에서 매개 변수로 Map in을 가져와야합니다 (개인적으로는 작업 명령을 선호합니다). – IdeaHat