C ++在类(在类中)中使用类并调用它们的函数

时间:2017-05-03 14:25:56

标签: c++ class

我遇到以下问题:我想重新设计一个项目,从好的CC++并制作所有内容class(y) :)并保持从一开始就可以扩展。

它是网格上的单元格(作为群体的一部分)的模拟,因此我决定了以下结构:

class Simulation has an instance of
class Grid       has an instance of
class Swarm      has an instance of
class Cell

我在单独的头文件中定义了这些类。当然,我还需要能够在grid,swarm和cell中调用函数。我想直接做到:

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;
}

只要网格没有发生变化,它就会调用该函数并运行。这些似乎在太空中丢失了。可能是指针错误,但我找不到错误,因为完全相同的代码适用于Simulation类......

更多来源:

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函数,但是群体永远不会出现,并且计数器不会在该网格中出现,只要函数在那里暂时就会暂时出现。我错过了什么吗?它真的只是指针错误吗?如果我这样称呼它,为什么这段代码会起作用:

Grid newgrid;
newgrid.create_swarm();

快速并购MWE

#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;
}

2 个答案:

答案 0 :(得分:0)

您的get_gridget_swarm方法会返回原始数组项的副本。您应该将引用(或指针)返回到GridSwarm

答案 1 :(得分:0)

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

您按价值返回,而不是通过引用返回。这意味着您要返回的是实际成员的副本。但是,在您的情况下,您希望通过引用返回,以便能够对原始对象进行更改。你的代码将成为

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

但请记住,您将无法以任何方式返回任何临时对象(例如您的dummy网格),因此您需要更改方法以避开此问题。如果你不想这样做,你仍然可以返回一个指针,虽然这会改变语法。