2017-05-03 1 views
0

다음과 같은 문제가 있습니다. 좋은 이전 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; 
} 
+0

가능한 경우 [MCVE]를 포함하십시오. – tambre

+2

C++에서 클래스를 반환하는 것은 "good old"C에서 구조체를 반환하는 것과 정확히 같습니다. 복사본을 만듭니다. – molbdnilo

+0

[mcve]가 없으면 말하기 어렵지만 문제는 벡터의 데이터에 대한 참조를 반환하지 않는 것 같습니다. – NathanOliver

답변

0
Grid Sim::get_grid(int grid_no) {...} 

귀하는 참조가 아닌 값으로 반환합니다. 다시 말해서 실제 회원의 복사본입니다. 그러나 원래 개체를 변경할 수 있으려면 참조로 돌아가고 싶을 것입니다. 코드는이 문제를 회피하기 위해 방법을 변경해야합니다 있도록

Grid& Sim::get_grid(int grid_no) {...} 

, 당신은 (예 : dummy 그리드로) 그런 식으로 어떤 임시직을 반환 할 수 없습니다 점을 염두에 두셔야 될 것입니다. 이렇게하지 않으려는 경우에도 구문을 조금 변경하더라도 포인터를 반환 할 수 있습니다.

0

귀하의 get_gridget_swarm 방법은 원래의 배열 항목의 복사본을 반환

Grid newgrid; 
newgrid.create_swarm(); 

신속 C & p'ed MWE : 왜 같이 호출하면이 코드 작업을 수행합니다. 대신 Grid 또는 Swarm에 대한 참조 (또는 포인터)를 반환해야합니다.

관련 문제