다음과 같은 문제가 있습니다. 좋은 이전 C
에서 C++
으로 프로젝트를 다시 실행하고 모든 것을 class(y) :)
으로 만들고 처음부터 확장 가능하게 유지하고 싶습니다.C++에서 클래스의 클래스를 사용하고 함수를 호출합니다.
그것은 격자 셀의 시뮬레이션 (떼의되는 부분)이므로, I는 다음의 구조를 결정했다
class Simulation has an instance of
class Grid has an instance of
class Swarm has an instance of
class Cell
는 I 별도 헤더 파일의 클래스를 정의. 그리드, 웜, 셀의 함수를 호출 할 수 있어야합니다. 나는 정직하고 싶었던 일 : 그리드와
Simulation mysim;
mysim.get_grid(0).any_function_here();
을 반환 매개 변수
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->test;//##//this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
그것은 함수를 호출 한 그리드의 변경이 이루어지지로 작동한다. 이들은 공간에서 잃어버린 것처럼 보인다.
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
이 나는대로 자주 create_swarm
함수를 호출 할 수 있습니다 : 같은 코드가 Simulation
클래스에 대한 작업을 정확하게 때문에 아마 포인터 오류가,하지만 난
더 많은 소스 ..., 오류를 찾을 수 없습니다 하지만 득실 거리는 곳은 나타나지 않고 카운터는 거기에있는 한 일시적으로 그리드에서 올라가지 않습니다. 내가 놓친 게 있니? 포인터 오류일까요?
#include <iostream>
#include <string>
#include <vector>
class Sim
{
public:
Sim();
virtual ~Sim();
Grid get_grid(int grid_no);
protected:
private:
std::vector<Grid> gridlist;
int amount_of_grids = -1;
};
class Grid
{
public:
Grid();
virtual ~Grid();
int set_size(int x, int y);
int create_swarm(std::string name);
Swarm get_swarm(int swarm_no);
void print_swarms();
protected:
private:
std::vector<Swarm> swarmlist;
int amount_of_swarms = -1;
/*static const*/ int size_x;
/*static const*/ int size_y;
std::vector<std::vector<Field>> fields;
std::string gridname;
};
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
using namespace std;
int main(int argc, char* argv[])
{
Sim mysim;
mysim.create_grid();
mysim.get_grid(0).create_swarm("Alpha-Swarm");
mysim.get_grid(0).create_swarm("Betaa-Swarm"); //doesn't work
Grid newgrid;
newgrid.create_swarm("Gamma-Swarm");
newgrid.create_swarm("Delta-Swarm"); // works, but is not needed.
return 0;
}
가능한 경우 [MCVE]를 포함하십시오. – tambre
C++에서 클래스를 반환하는 것은 "good old"C에서 구조체를 반환하는 것과 정확히 같습니다. 복사본을 만듭니다. – molbdnilo
[mcve]가 없으면 말하기 어렵지만 문제는 벡터의 데이터에 대한 참조를 반환하지 않는 것 같습니다. – NathanOliver